Technology3 min read

How to flatten a list of list in Python?

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Python Lists

Summarize this article using AI

Consider the below list of lists:

my_list = [[1,2], [3,4], ['a', 'b'], [1.23, 5.73]]

What if you wish to flatten this list, i.e get all the elements in a single list rather than have multiple lists inside a master list?

This can be done in various ways in python, and we will cover a few of them in this post.




1. Using a new list

The below method creates a new list, and the old list of lists is iterated through and every element present in the list of lists is appended to the new list.

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]
flat_list = []
for sublist in my_list:
    for item in sublist:
        flat_list.append(item)
print(flat_list)

Output:

[1, 2, 'abc', 'studytonight', 'a', 'b', 1.23, 5.73]



2. Using list comprehension

List comprehension can be used to flatten a list of lists. It has been demonstrated below:

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]
flat_list = [item for sublist in my_list for item in sublist]
print(flat_list)

Output:

[1, 2, 'abc', 'studytonight', 'a', 'b', 1.23, 5.73]



3. Using the iterable method from the itertools module

import itertools

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]
flat_list = list(itertools.chain.from_iterable(my_list))
print(flat_list)

Output:

[1, 2, 'abc', 'studytonight', 'a', 'b', 1.23, 5.73]



4. Using the concat method from functools

import functools

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]
functools.reduce(operator.concat, my_list)

Output:

[1, 2, 'abc', 'studytonight', 'a', 'b', 1.23, 5.73]

Note: Instead of the concat method in the above code, iconcat method also can be used. It flattens the list of lists and gives the same output as that of using the concat method.




5. Using the numpy module's flat option

If you are new to numpy, here is an introduction to Numpy for beginners.

import numpy

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]
list(numpy.array(my_list).flat)

Output:

['1', '2', 'abc', 'studytonight', 'a', 'b', '1.23', '5.73']



6. Using the concatenate method from the numpy module

import numpy

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]
list(numpy.concatenate(my_list))

Output:

['1', '2', 'abc', 'studytonight', 'a', 'b', '1.23', '5.73']



7. Using the reduce method

from functools import reduce

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]
reduce(lambda x,y: x+y, my_list)

Output:

[1, 2, 'abc', 'studytonight', 'a', 'b', 1.23, 5.73]



8. Using the iterable method from the collections module

my_list = [[1,2], ['abc', 'studytonight'], ['a', 'b'], [1.23, 5.73]]

from collections import Iterable

def flatten(items):
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
            yield from flatten(x)
        else:
            yield x
list(flatten(my_list))

Output:

[1, 2, 'abc', 'studytonight', 'a', 'b', 1.23, 5.73]



Conclusion

In this post, we saw different ways in which a list of a list can be flattened. Do let us know whether you have a different approach to it.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next