Back to articles list Articles
10 minutes read

Printing a List in Python – 10 Tips and Tricks

Do you know there are many ways to print a list in Python? This article will explore some of my best tips and tricks for printing a list in Python.

The Python print() function is an essential built-in function, and it is important to use alongside Python lists.

To learn how a Python list works, what kind of data structure it is, and why it is essential to Python and programming in general, it's best to take our Python Data Structure course. It has 118 interactive exercises, and you only need a web browser and some basic Python knowledge. When you finish it, you'll understand lists, tuples, sets, and dictionaries in Python. 

If you already have some knowledge of Python data structures, here are some tips and tricks on how to print a list in Python.

10+ Ways to Print a List in Python

1. Print a list using *

To print a list using the * operator, you can use the print() function as follows:

print(*list_name)

This will print the elements of the list separated by spaces, just like when you use the print() function with multiple arguments. For example:

pokemon_list = ['Pikachu', 'Abra', 'Charmander']
print(*pokemon_list)

This will print:

Pikachu Abra Charmander

You can also use the * operator to unpack a list when calling a function that takes multiple arguments:

def print_pokemon(pokemon1, pokemon2, pokemon3):
   print(pokemon1)
   print(pokemon2)
   print(pokemon3)
  
print_pokemon(*pokemon_list)

This will output:

Pikachu
Abra
Charmander

2. Print a List in One Line

It is also possible to print a list in one line of code using this syntax:

print(item for item in list_name) 

To do so, you can use a generator expression inside the print() function.

A generator expression is similar to a list comprehension, but it returns a generator object that produces the values one at a time (instead of creating a new list). Generator expressions and list comprehensions are Python one-liners – i.e. they are one-line Python programs.

Here's an example of how you can use a generator expression to print a Python list:

pokemon_list = ['Pikachu', 'Abra', 'Charmander']

This will print the elements of the list separated by spaces, just like when you use the print() function with multiple arguments:

Pikachu Abra Charmander

You can also use a generator expression to unpack a list when calling a function that takes multiple arguments. For example:

def print_pokemon(pokemon1, pokemon2, pokemon3):
   print(pokemon1)
   print(pokemon2)
   print(pokemon3)

print_pokemon(*(item for item in pokemon_list))

Output:

Pikachu
Abra
Charmander

Keep in mind that generator expressions are usually used when you only need to iterate over the values once; they are not stored in memory like lists are. If you need to access the values of the list multiple times, it might be more efficient to use a list comprehension or to store the values in a list.

3. Print a Python List With a Separator Between Items

To print a list with a separator between list items, you can use a list comprehension inside a print() function:

print(*[item + '\n' for item in pokemon_list])

The newline character \n at the end of each element ensures each element will print on a separate line:

Pikachu
 Abra
 Charmander

Remember that you can use any string as a separator, not just a newline character. For example, to print a list in Python with a comma and a space between each element, you can use the following syntax:

print(*[item + ', ' for item in pokemon_list])

It returns:

Pikachu,  Abra,  Charmander,

Note that the last element will have a comma and a space after it, which might differ from what you want.

4. Print a List in Python Without a Separator at the End

To avoid this pitfall, you can use an if statement inside a list comprehension to only add the separator after the elements that are not last in the list. Here’s how it looks:

print(*[item + (', ' if i < len(pokemon_list)-1 else '') for i, item in enumerate(pokemon_list)])

Output:

Pikachu,  Abra,  Charmander

To print a list with a separator between the list items but without the separator at the end, you can also use the print() function with the sep keyword argument:

print(*pokemon_list, sep=', ')

This will print the elements separated by a comma and a space:

Pikachu, Abra, Charmander

Note that the print() function automatically adds a space after each element, so you don't need to include it in the separator string.

Similar to what I mentioned previously, you can use any string as a separator, such as a dash:

print(*pokemon_list, sep='-')

This will print:

Pikachu-Abra-Charmander

Remember that the sep keyword argument only affects the separator between elements, not the characters printed after the last element.

Suppose you want to suppress the space automatically added after the last element. In that case, you can use the end keyword argument to specify a different string to be printed after the last one:

print(*pokemon_list, sep=', ', end='')

This will print:

Pikachu, Abra, Charmander

5. Print a Python List Using join

To print a list using the join() method in Python, you can use the following syntax:

print(separator.join(list_name))

This will join the list elements with the separator string between them. For example, to print a list of pokemons separated by a comma and a space, you can do the following:

print(', '.join(pokemon_list))

This will print:

Pikachu, Abra, Charmander

If you want to add a string before or after the list, you can use the + operator to concatenate the strings:

print('Pokemons: ' + ', '.join(pokemon_list))

This will print:

Pokemons: Pikachu, Abra, Charmander

6. Convert and Print a List

To print a list by converting it to a string in Python, you can use the str() function:

print(str(pokemon_list))

This will print the list as a string, with the elements separated by commas and enclosed in square brackets:

['Pikachu', 'Abra', 'Charmander']

Next, let’s explore how to print a Python list while indexing the list elements.

7. Print and Index a Python List

To print the items of a list with their index, you can use the Python function enumerate() with a for loop:

for i, pokemon in enumerate(pokemon_list):
   print(i, pokemon)

This will output:

0 Pikachu
1 Abra
2 Charmander

The enumerate() function returns an iterator that produces tuples containing the index and the value of each element of the list. You can use a for loop to iterate over these tuples and print the index and value of each element.

Using a variation of the previous way we did string formatting, we can customize how the index and value are printed. For example:

for i, pokemon in enumerate(pokemon_list):
    print('{}: {}'.format(i, pokemon))

This will output:

0: Pikachu
1: Abra
2: Charmander

You can also use a list comprehension to create a new list of strings with the formatted index and value of each element and then use the print() function to print the list:

result = ['{}: {}'.format(i, pokemon) for i, pokemon in enumerate(pokemon_list)]
print(result)

This will output:

['0: Pikachu', '1: Abra', '2: Charmander']

Remember that the enumerate() function returns the index of each element starting from 0. If you want the index to start from a different value, you can pass it as a function argument. For example:

for i, pokemon in enumerate(pokemon_list, 1):
   print('{}: {}'.format(i, pokemon))

This will output:

1: Pikachu
2: Abra
3: Charmander

8. Print Two Python Lists Together

To print two lists together, with the elements of each list interleaved, you need to loop over multiple lists. For this purpose, you can use a for loop and the zip() function.

The zip() function returns an iterator that produces tuples containing the elements at their corresponding positions inside the list. You can use a Python loop to iterate over these tuples and print the elements of each list.

Here's an example:

a = ['a1', 'a2', 'a3']
b = ['b1', 'b2', 'b3']
for x, y in zip(a, b):
    print(x, y)

This will output:

a1 b1
a2 b2
a3 b3

You can also use a list comprehension to create a new list with the interleaved elements and then use the print() function to print it:

a = ['a1', 'a2', 'a3']
b = ['b1', 'b2', 'b3']
result = [x + y for x, y in zip(a, b)]
print(result)

This will output:

['a1b1', 'a2b2', 'a3b3']

It is important to remember that the zip() function will only iterate over the elements of the lists until the shortest list is exhausted. If the lists have different lengths, the elements of the longer list will not be included in the output. For example:

a = ['a1', 'a2', 'a3']
b = ['b1', 'b2']
for x, y in zip(a, b):
    print(x, y)

This will return:

a1 b1
a2 b2

If you want to include all the elements of the longer list, you can pad the shorter list with None values using the itertools.zip_longest() function from the itertools module. For example:

import itertools

a = ['a1', 'a2', 'a3']
b = ['b1', 'b2']

for x, y in itertools.zip_longest(a, b):
    print(x, y)

This will output:

a1 b1
a2 b2
a3 None

9. Print a Nested Python List

To print a nested list in Python, you can use a loop to iterate over the elements of the outer list and then use another loop to iterate over the elements of the inner list(s). For example:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for sublist in nested_list:
   for item in sublist:
       print(item)

This will output:

1
2
3
4
5
6
7
8
9

We can also print each list in separate lines:

We can also print each list in separate lines:
>>> nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> for sublist in nested_list:
...     print(*sublist, sep=",")

This will return:

1,2,3
4,5,6
7,8,9

Next, we can use a list comprehension to create a new list with the elements of the nested list and then use the print() function to print the list:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = [item for sublist in nested_list for item in sublist]

This will output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Lists are powerful and can also be combined with sets and other Python data structures.

10+. There’s Always More …

Here are a few more ways to print lists in Python:

  • Using a for loop and the range() function:
for i in range(len(pokemon_list)):
   print(pokemon_list[i])

This will output:

Pikachu
Abra
Charmander
  • Another option is to use the pprint module, which provides a pprint() function to pretty-print lists and other data structures:
import pprint

pokemon_list = ['Pikachu', 'Abra', 'Charmander']

pprint.pprint(pokemon_list)

This will output:

['Pikachu', 'Abra', 'Charmander']
  • It is also possible to use the json module, which provides a dumps() function that can convert a list to a JSON string:
import json

pokemon_list = ['Pikachu', 'Abra', 'Charmander']

print(json.dumps(pokemon_list))

This will output:

["Pikachu", "Abra", "Charmander"]
  • Last but not least, you can also use the f-strings feature (available in Python 3.6 and later).This feature allows you to embed expressions inside string literals using the {} placeholder:
for pokemon in pokemon_list:
   print(f'{pokemon} is a pokemon')

This will output:

Pikachu is a pokemon
Abra is a pokemon
Charmander is a pokemon

Beyond Printing Lists in Python

In this article, we explored many ways of printing a list in Python. But there's more, such as printing a Python list sorted alphabetically.

As you’ve seen in this article, there are plenty of ways to work with strings in Python. You can take advantage of our course on Working with Strings in Python to expand your knowledge about this important data type.

Feel free to read more content on LearnPython.com to keep learning. And if you haven't started your journey yet, now is a good time to start programming in Python!