Convert a list of tuples to a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 31, 2022

banner

Photo from Unsplash

Convert a list of tuples to a String in Python #

Use the str() class to convert a list of tuples to a string, e.g. my_str = str(list_of_tuples). If you need to remove or change the delimiter, flatten the list and use the str.join() method.

main.py
list_of_tuples = [(2, 4), (6, 8), (1, 3)] # ✅ Convert list of tuples to string my_str = str(list_of_tuples) print(my_str) # 👉️ [(2, 4), (6, 8), (1, 3)] # ----------------------------- # ✅ Convert list of tuples to string without square brackets my_str = str(list_of_tuples).strip('[]') print(my_str) # 👉️ (2, 4), (6, 8), (1, 3) # ----------------------------- # ✅ Convert list of tuples to string without square brackets and parentheses my_str = ' '.join(str(item) for tup in list_of_tuples for item in tup) print(my_str) # 👉️ 2 4 6 8 1 3 # ----------------------------- my_str = ','.join(str(item) for tup in list_of_tuples for item in tup) print(my_str) # 👉️ 2,4,6,8,1,3

The first example uses the str() class to convert a list of tuples to a string.

main.py
list_of_tuples = [(2, 4), (6, 8), (1, 3)] my_str = str(list_of_tuples) print(my_str) # 👉️ [(2, 4), (6, 8), (1, 3)]
The str() class converts the list of tuples to a string and keeps the square brackets, parentheses and delimiters.

If you need to remove the square brackets, use the str.strip() method.

main.py
list_of_tuples = [(2, 4), (6, 8), (1, 3)] my_str = str(list_of_tuples).strip('[]') print(my_str) # 👉️ (2, 4), (6, 8), (1, 3)

The str.strip method returns a copy of the string with the provided leading and trailing characters removed.

main.py
my_str = "[(2, 4), (6, 8)]".strip('[]') print(my_str) # 👉️ '(2, 4), (6, 8)'

If you need to remove or change the delimiter between the tuple's elements, flatten the list of tuples and use the str.join() method.

main.py
list_of_tuples = [(2, 4), (6, 8), (1, 3)] my_str = ' '.join(str(item) for tup in list_of_tuples for item in tup) print(my_str) # 👉️ 2 4 6 8 1 3 my_str = ','.join(str(item) for tup in list_of_tuples for item in tup) print(my_str) # 👉️ 2,4,6,8,1,3

We used a generator expression to flatten the list of tuples.

main.py
list_of_tuples = [(2, 4), (6, 8), (1, 3)] my_str = list(str(item) for tup in list_of_tuples for item in tup) print(my_str) # 👉️ ['2', '4', '6', '8', '1', '3']
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

The last step is to use the str.join() method to join the list into a string.

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

main.py
list_of_tuples = [(2, 4), (6, 8), (1, 3)] my_str = ','.join(str(item) for tup in list_of_tuples for item in tup) print(my_str) # 👉️ 2,4,6,8,1,3
Note that the method raises a TypeError if there are any non-string values in the iterable.

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

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
list_of_tuples = [(2, 4), (6, 8), (1, 3)] my_str = ''.join(str(item) for tup in list_of_tuples for item in tup) print(my_str) # 👉️ '246813'

If your list of tuples contains strings, you don't have to use the str() class.

main.py
list_of_tuples = [('a', 'b'), ('c', 'd'), ('e', 'f')] my_str = ','.join(item for tup in list_of_tuples for item in tup) print(my_str) # 👉️ 'a,b,c,d,e,f'
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.