Convert a list of tuples to a list in Python

avatar

Borislav Hadzhiev

Last updated: Jul 2, 2022

banner

Photo from Unsplash

Convert a list of tuples to a list in Python #

To convert a list of tuples to a list:

  1. Declare a new variable and set it to an empty list.
  2. Use a for loop to iterate over the list of tuples.
  3. Use a nested for loop to iterate over each tuple.
  4. Append each item to the empty list.
main.py
list_of_tuples = [('a', 'b'), ('c', 'd'), ('e', 'f')] my_list = [] for tup in list_of_tuples: for item in tup: my_list.append(item) # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(my_list)

We used a nested for loop to convert a list of tuples to a list.

The outer for loop is used to iterate over the list.

In order to get access to the items in the tuples, we need a nested for loop.

On each iteration, we append each tuple element into an empty list.

You could use this approach with one more nested loop if you had a more deeply nested list.

You can also use a list comprehension to achieve the same result.

main.py
list_of_tuples = [('a', 'b'), ('c', 'd'), ('e', 'f')] my_list = [item for tup in list_of_tuples for item in tup] # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(my_list)

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The list comprehension in the example is very similar to the for loop we used, but the syntax is a bit harder to read.

We still have 2 for loops but instead of appending the items of the tuple to an empty list, we simply return them from the list comprehension.

Alternatively, you can use the itertools.chain.from_iterable method.

Use the itertools.chain.from_iterable() method to convert a list of tuples to a list, e.g. list(itertools.chain.from_iterable(list_of_tuples)). The method uses a nested for loop under the hood and joins the list of tuples into a single flat list.

main.py
import itertools list_of_tuples = [('a', 'b'), ('c', 'd'), ('e', 'f')] my_list = list(itertools.chain.from_iterable(list_of_tuples)) # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(my_list)

The itertools.chain.from_iterable method can be used to achieve the same result.

The method is roughly equivalent to the following function.

main.py
def from_iterable(iterables): for it in iterables: for element in it: yield element

Another alternative to consider is to use the built-in sum() function.

To convert a list of tuples to a list:

  1. Pass the list of tuples and an empty tuple to the sum() function.
  2. The sum function will sum the elements of the list and use the empty tuple as the start value.
  3. Use the list() class to convert the result to a list.
main.py
list_of_tuples = [('a', 'b'), ('c', 'd'), ('e', 'f')] my_list = list(sum(list_of_tuples, ())) # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(my_list)

The sum function takes an iterable and a start argument, sums them from left to right and returns the total.

When we sum two tuples, we get a single tuple containing the elements from the two tuples.

main.py
print(('one',) + ('two',)) # 👉️ ('one', 'two') print((1, 3) + (5, 7)) # 👉️ (1, 3, 5, 7)
By default the start argument is set to 0, so we have to explicitly pass an empty tuple.

The last step is to use the list() class to convert the tuple to a list.

Note that this approach is a bit more inefficient than using a nested for loop or the itertools.chain.from_iterable method.

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.