Print a list of tuples in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

Print a list of tuples in Python #

To print a list of tuples:

  1. Use a for loop to iterate over the list.
  2. Access specific elements in each tuple.
  3. Use the print() function to print the result.
main.py
list_of_tuples = [ (1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada'), ] for tup in list_of_tuples: # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada print(tup[0], tup[1], tup[2])

We used a for loop to iterate over the list of tuples and accessed specific elements in each tuple.

You can use the iterable unpacking operator if you need to print all elements of each tuple.
main.py
list_of_tuples = [ (1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada'), ] for tup in list_of_tuples: # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada print(*tup)

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

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

main.py
list_of_tuples = [ (1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada'), ] for tup in list_of_tuples: # Name: alice | Country: Austria # Name: bobbyhadz | Country: Bulgaria # Name: carl | Country: Canada print(f'Name: {tup[1]} | Country: {tup[2]}')
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 same approach if you need to print the list of tuples in
tabular format.

main.py
headers = [ 'ID', 'Name', 'Country' ] list_of_tuples = [ (1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada'), ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in list_of_tuples: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'

The space between the colon and the less-than sign is the fill character.

The less-than or greater-than sign is the alignment.

The less-than sign aligns the string to the left and the greater-than sign aligns the string to the right.

We first format and print the headers and then iterate over the list and print each row.

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

main.py
headers = [ 'ID', 'Name', 'Country' ] list_of_tuples = [ (1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada'), ] # 1, alice, Austria # 2, bobbyhadz, Bulgaria # 3, carl, Canada for tup in list_of_tuples: result = ', '.join(str(item) for item in tup) 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 a list of tuples into a string, use two calls to the str.join() method.

main.py
list_of_tuples = [ (1, 'alice'), (2, 'bobbyhadz'), (3, 'carl'), ] result = ', '.join(' '.join(str(element) for element in tup) for tup in list_of_tuples) print(result) # 👉️ 1 alice, 2 bobbyhadz, 3 carl
The inner call to the join() method joins the elements of the tuple of the current iteration.

We used a space separator when joining the elements of each tuple, 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 tuples in the list into a string with a comma separator.

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.