Python
Container Types
list
print

Python List Print: Explanation and Examples

Today we're going to learn all about Python list Print.

When you're writing code to print a list, sometimes you want it to look a certain way. In this case, print(my_list) is often not enough.

So today we're going to look at different ways to print lists to meet these needs. This will include

  • Formatting the list
  • Inserting line breaks
  • Without brackets, quotes or spaces
  • Using the * operator
  • Indexed output
  • Output in reverse order

1. Formatting

The simplest way to print a list is to use the print() function. However, this method prints the list as it is, so we need to do some extra work to make it look the way we want.

For example, you may want to put a certain delimiter between the items in the list, or you may want to sort the items in a certain way when you print the list.

In this case, you can do something like the following example.

my_list = ['apple', 'banana', 'cherry']
 
print(f"{' '.join(my_list)}")
 
# Output
apple banana cherry

In the code above, the join function is responsible for concatenating all the elements in the list into a specific string. f-string is a feature supported since Python 3.6 that allows you to insert the value of a variable directly into a string.

💡

If you want to learn more about f-strings, see this post.

2. Printing with line breaks

If you want to print the elements of a list one per line, you can use the join method and put \n between the elements. The \n is an escape character that means new line.

print('\n'.join(my_list))
 
# Output
apple
banana
cherry

When you run the above code, each item in the list is output with a newline as the separator.

3. Printing without brackets

By default, when you print a list in Python, it is printed with brackets. However, sometimes you may want to print just the elements of a list without the brackets.

In that case, try this.

print(' '.join(my_list))
 
# Output
apple banana cherry

The above code prints each element of the list separated by a space. You can use the join function to join the elements of a list without parentheses.

4. Printing without spaces

If you want to print a list without spaces, you can join it with '' (an empty string).

print(''.join(my_list))
 
# Output
applebananacherry

When you run the code above, all items in the list are concatenated without spaces, meaning there are no characters between them.

5. Using * for output

In Python, * is called the unpacking operator.

You can use it to unpack and print all the elements of a list or tuple. Unpacking refers to separating the elements of a collection data type one at a time.

print(*my_list)
 
# Output
apple banana cherry

When you run the code above, each item in the list is printed individually with whitespace between them.

As we saw in the Join lists post, it can also be used to join two lists.

print(*my_list, *['are', 'yummy'] )
 
# Output
apple banana cherry are yummy

6. Printing without quotes

To print each element of a list without quotes, you can use the map() function, usually in conjunction with the str.join() method. The map() function returns the result of applying a given function to all elements of a collection, which is useful when the list elements are not strings.

print(' '.join(map(str, my_list)))
 
# Output
apple banana cherry

When you run the code above, all elements in the list are printed without quotes. The map() function converts each element of the list to a string, and the join() function concatenates the strings with a space.

7. Printing with indexes

If you want to print the index of a list, you can use the enumerate() function. The enumerate function returns each element of the collection and its index in a tuple.

for i, v in enumerate(my_list):
    print(f'index {i}: value {v}')
 
# Output
index 0: value apple
index 1: value banana
index 2: value cherry

When you run the above code, it will print each element of the list along with its index.

8. Printing in reverse order

The final method is to print the list in reverse order.

You can use the reversed() function to print the elements of a list in reverse order. This function returns the elements of the given collection in reverse order.

for i in reversed(my_list):
    print(i)
 
# Output
cherry
banana
apple

Alternatively, you can use a slicing syntax.

for i in my_list[::-1]:
    print(i)
 
# Output
cherry
banana
apple

The last number in the slicing syntax for a Python list is the step or interval that, if set to -1, will return the list in reverse order.

When you run the code above, the elements of the list are printed in reverse order.

Conclusion

We've seen several ways to print a list in Python. Most of them use the join() function.

I hope you'll use this as a starting point to experiment with your own output formats.

copyright for Python List Print: Explanation and Examples

© 2023 All rights reserved.