Different Ways to Print Lists in Python?

How do print lists in Python? To print lists you can use looping, list comprehension, join(), and many other ways. We are often required to print the list as a string, vertically, every element as a new line etc.

Methods to print lists in Python:

  1. Using * operator
  2. Iterating List with looping
  3. Using join() print as a string
  4. Using list comprehension
  5. Printing list with a newline or vertically

1. Quick Examples of Printing Lists


# Quick Examples of printing lists

# Print list with space separator 
print(*numbers)

# Print with comma separator
print(*numbers, sep = ",") 

# Print every element end with command and new line 
print(*numbers, sep = ",\n") 

# Printing numbers as a string
print(' '.join(map(str, numbers)))

# Using list comprehension
[print(i, end=' ') for i in numbers] 

# Using for loop
for x in numbers:
    print(x)

2. Printing Lists in Python

As I said there are several ways to print lists in Python, To start with a simple example, we can use the * operator to print elements of the list with a space separator. You can also use this to display with comma, tab, or any other delimiter while printing lists.


# Create Numbers List
numbers = [11,12,13,14,15,16,17]
print(numbers)

# Using * Operator
# Print list with space separator 
print(*numbers)

# Print comma separator
print(*numbers, sep = ",") 

# Print comma with additional space
print(*numbers, sep = ", ") 

Yields below output. If you want each element in a new line, you can also use a separator \n.

python print lists

3. Print List using Loop

You can also use the looping to print elements from the list. Here, I used for in list syntax to get each element from the list and print it on the console.


# Create Numbers List
numbers = [11,12,13,14,15,16,17]

# Print list with loop
for x in numbers:
    print(x)

I will leave this to you to run and explore the output. Similarly, you can also use other loops in python like the while loop to get a similar output.

4. Printing List as a String

If you wanted to print the list as a string, you can use the join() to convert the list to a string and print it as a string. The join() can be used only with strings hence, I used map() to convert the number to a string first and then used the join to combine.

In the second example, I used the list of strings hence map() was not used.


# Create Numbers List
numbers = [11,12,13,14,15,16,17]

# Printing numbers as a string
print(' '.join(map(str, numbers))) 

# Output:
# 11 12 13 14 15 16 17

# Printing string as a single string
mylist = ['Apple','Mango','Guava','Grape']    
mystring = ' '.join(mylist)
print(mystring)

# Output:
#Apple Mango Guava Grape

5. Print List Comprehension


# Using list comprehension
[print(i, end=' ') for i in numbers] 

# Output:
#11 12 13 14 15 16 17 

6. Print List Vertically

Pretty much all the above methods you can use to print the python list vertically (every element in a newline). When using * operator and join() method you can use \n separator to print vertically, and using a loop with print() by default display every element as a new line. Here are the examples.


# Printing vertically using * operator
print(*numbers, sep = "\n") 

# Printing vertically using join
print('\n'.join(numbers))

# Using list comprehension
[print(i) for i in numbers]

# Printing using looping.
for x in numbers
   print(x)

Yields below output.

python display list

7. Conclusion

In this article, you have learned different ways to print lists in Python. Learned to use * operator, for loop, printing list as a string, printing vertically, and many more example.

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply