Signup/Sign In

How to flatten a list of list in Python?

Posted in Technology   LAST UPDATED: OCTOBER 31, 2019

    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.

    Author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    PythonPython Lists
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS