Print a list of lists on separate lines in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

Print a list of lists on separate lines in Python #

To print a list of lists on separate lines:

  1. Use a for loop to iterate over the list.
  2. Use the iterable unpacking operators to unpack the items of each sublist.
  3. Use the print() function to print the result.
main.py
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] for sublist in list_of_lists: # 1 alice # 2 bobbyhadz # 3 carl print(*sublist)

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

If you need to only print specific items of each sublist, access them at index.

main.py
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] for sublist in list_of_lists: # alice # bobbyhadz # carl print(sublist[1])

You can use a formatted string literal if you need to format each row in a specific way.

main.py
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] for sublist in list_of_lists: # id: 1 | name: alice # id: 2 | name: bobbyhadz # id: 3 | name: carl print(f'id: {sublist[0]} | name: {sublist[1]}')
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # 👉️ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

You can use the str.join() method if you need to join the elements of each sublist with a delimiter.

main.py
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] # 1, alice # 2, bobbyhadz # 3, carl for sublist in list_of_lists: result = ', '.join(str(item) for item in sublist) print(result)

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your iterable contains numbers or other types, convert all of the values to strings before calling join().

The string the method is called on is used as the separator between the elements.

We used a comma and a space (, ) in the example, but you can use any other delimiter.

If you need to join the list of lists into a string that contains newline (\n) characters, use two calls to the str.join() method.

main.py
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] result = '\n'.join(' '.join(str(item) for item in sublist) for sublist in list_of_lists) # 1 alice # 2 bobbyhadz # 3 carl print(result)
The inner call to the join() method joins the elements of the sublist of the current iteration.

We used a space separator when joining the elements of each sublist, but you can use any other delimiter.

We used the str() class to convert each value to a string before calling join().

The last step is to use the join() method to join the sublists in the list into a string with a newline (\n) character separator.

The items of each nested list are printed on a separate line because of the newline characters.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.