Print a list of tuples without Brackets in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

Print a list of tuples without Brackets in Python #

To print a list of tuples without brackets:

  1. Use a for loop to iterate over the list.
  2. Use the str.join() method to join the elements of each tuple into a string.
  3. Use the print() function to print the result.
main.py
list_of_tuples = [ (1, 'alice'), (2, 'bobbyhadz'), (3, 'carl'), ] # ✅ print list of tuples without brackets (for loop) # 1, alice # 2, bobbyhadz # 3, carl for tup in list_of_tuples: result = ', '.join(str(item) for item in tup) print(result) # ---------------------------------------------------- # ✅ print list of tuples without brackets (str.join()) result = ', '.join(' '.join(str(element) for element in tup) for tup in list_of_tuples) print(result) # 👉️ 1 alice, 2 bobbyhadz, 3 carl # ---------------------------------------------------- # ✅ print list of tuples without brackets, with parenthesis # 👇️ (1, 'alice'), (2, 'bobbyhadz'), (3, 'carl') print(str(list_of_tuples)[1:-1])

The first example uses a for loop to iterate over the list of tuples.

On each iteration, we use the str.join() method to join the tuple's elements into a string.

main.py
list_of_tuples = [ (1, 'alice'), (2, 'bobbyhadz'), (3, 'carl'), ] # 1, alice # 2, bobbyhadz # 3, carl 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.

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

The example joins the elements of each tuple with a space separator.

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.

If you need to print a list of tuples without the square brackets, but with the parenthesis, convert the list to a string and use string slicing to exclude the square brackets.

main.py
list_of_tuples = [ (1, 'alice'), (2, 'bobbyhadz'), (3, 'carl'), ] # 👇️ (1, 'alice'), (2, 'bobbyhadz'), (3, 'carl') print(str(list_of_tuples)[1:-1])

We used the str() class to convert the list of tuples to a string and used string slicing to exclude the square brackets.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

We used a start index of 1 to exclude the left square bracket and used a stop index of -1 to exclude the right square bracket.

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.