[Python] Display only the elements of the list side by side [Vertical, horizontal]

Introduction

I think that there are many cases where the output method is limited due to online programming learning, skill check, etc. Even though I got the answer, sometimes I couldn't output it as I expected, so I summarized what I investigated.

Display the elements of the list vertically

[Method 1] Put print () in the list comprehension

ex.py


L = ["a","b","c","d"]

[print(i) for i in L]

[Method 2] Slice the character string character by character and insert print ().

ex.py


L = ["a","b","c","d"]

for i in L[0:]:
print(i)

In any case, the execution result will be as follows.

a b c d

Display the elements of the list side by side

Open a space and display by using .join ()

ex.py


L = ["a","b","c","d"]

L=' '.join(L)
print(L)

The execution result will be as follows

a b c d

With ','. join

a,b,c,d

The element in'' before .join () can be set freely so that

Method without .join ()

You can output without using .join ().

ex.py


L = [['a', 'b', 'c', 'd']]
print(*L[0])

Execution result

a b c d

.Join () cannot be used with numbers (int values)

ex.py


L_int = [1, 2, 3, 4]

L=[str(a) for a in L_int]
L=" ".join(L)
print(L)

The execution result will be as follows

1 2 3 4

Finally

I would be grateful if you could tell me if there is any method other than those described this time.

Recommended Posts

[Python] Display only the elements of the list side by side [Vertical, horizontal]
[python] How to display list elements side by side
[python] Check the elements of the list all, any
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
Get the number of specific elements in a python list
2015-11-26 python> Display the function list of the module> import math> dir (math)
About the basics list of Python basics
Display a list of alphabets in Python 3
[Python] Display list elements with variadic arguments
Pandas of the beginner, by the beginner, for the beginner [Python]
[Python] Get a list of folders only
Extension of Python by C or C ++ (when there are multiple arguments, when passing a list from the Python side)
[Python] Sort the list of pathlib.Path in natural sort
Sort the elements of the array by specifying the conditions
Make a copy of the list in Python
Get only the subclass elements in a list
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
Search by the value of the instance in the list
This is the only basic review of Python ~ 3 ~
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
List of python modules
Gradually display the output of the command executed by subprocess.Popen
[python] Get the list of classes defined in the module
[Python] Manipulation of elements in list (array) [Add / Delete]
the zen of Python
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
How to check in Python if one of the elements of a list is in another list
[Maya Python] Crush the contents of the script 2 ~ list Notes
Display of fractions (list)
Make sure all the elements in the list are the same in Python
Try to get the function list of Python> os package
[Python] How to make a list of character strings character by character
Make the display of Python module exceptions easier to understand
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
Sort and output the elements in the list as elements and multiples in Python.
[Python3] Call by dynamically specifying the keyword argument of the function
Debug by attaching to the Python process of the SSH destination
Mass generation of QR code with character display by Python
Create a new list by combining duplicate elements in the list
Find the diameter of the graph by breadth-first search (Python memory)
Towards the retirement of Python2
Summary of Python3 list operations
About the ease of Python
[Python] Copy of multidimensional list
Getting list elements in Python
About the features of Python
The Power of Pandas: Python
Python> Difference in content display of multiline strings> print / automatic echoing done by the interactive interpreter
[Understanding in the figure] Management of Python virtual environment by Pipenv
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Python] Let's reduce the number of elements in the result in set operations
Read the standard output of a subprocess line by line in Python
[Python of Hikari-] Chapter 05-03 Control syntax (for statement-extracting elements from list-)
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
[python] Get the rank of the values in List in ascending / descending order
python note: map -do the same for each element of the list
List of disaster dispatches from the Sapporo City Fire Department [Python]
[Python] A program that rotates the contents of the list to the left