Open In App

Print a List in Python Horizontally

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Printing a list in Python is a common task, and there are several ways to display the elements horizontally. In this article, we will explore some different simple and commonly used methods to achieve this. Each method will be accompanied by code examples to illustrate their implementation.

How to Print a List in Python Horizontally?

Below, are the methods of How To Print A List In Python Horizontally.

Print a List In Python Horizontally Using Print Statement

The * operator, also known as the unpacking operator, can be used to print the elements of a list separated by spaces. This method is concise and effective.

Python3




# Using the Print Statement with Separator
my_list = [1, 2, 3, 4, 5]
 
print(*my_list, sep=' ')


Output

1 2 3 4 5

Print a List In Python Horizontally Using a For Loop

In this method, we iterate through each element in the list using a for loop. The end=' ' argument in the print statement ensures that the elements are printed horizontally with a space between them.

Python3




# Using a For Loop
my_list = [1, 2, 3, 4, 5]
 
for element in my_list:
    print(element, end=' ')


Output

1 2 3 4 5 

Print a List In Python Horizontally Using Join() Method

Here, we use the join method to concatenate the elements of the list into a string, and map(str, my_list) is used to convert each element to a string before joining.

Python3




# Using the Join Method
my_list = [1, 2, 3, 4, 5]
 
print(' '.join(map(str, my_list)))


Output

1 2 3 4 5

Print a List In Python Horizontally Using List Comprehension

In this example, below code using list comprehension to convert each element of the list into a string and then joins these strings with a space using the `join` method. This concise approach combines the power of list comprehensions with the flexibility of the `join` method.

Python3




# Using List Comprehension and Join
my_list = [1, 2, 3, 4, 5]
 
print(' '.join([str(element) for element in my_list]))


Output

1 2 3 4 5

Conclusion

In conclusion, printing a list horizontally in Python can be achieved by using the ‘end’ parameter in the print() function. By setting ‘end’ to an empty string, elements are displayed on the same line with spaces in between. Alternatively, the ‘join’ method can be employed to concatenate list elements into a single string, which can then be printed in a horizontal fashion. Overall, these approaches offer flexibility and ease in presenting lists in a horizontal format in Python



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads