in Technology by
How to make a flat list out of list of lists in Python?

1 Answer

0 votes
by

Given a list of lists t,

flat_list = [item for sublist in t for item in sublist]

which means:

flat_list = []
for sublist in t:
    for item in sublist:
        flat_list.append(item)

is faster than the shortcuts posted so far. (t is the list to flatten.)

Here is the corresponding function:

flatten = lambda t: [item for sublist in t for item in sublist]

As evidence, you can use the timeit module in the standard library:

$ python -mtimeit -s't=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in t for item in sublist]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s't=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(t, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s't=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x,y: x+y,t)'
1000 loops, best of 3: 1.1 msec per loop

Explanation: the shortcuts based on + (including the implied use in sum) are, of necessity, O(T**2) when there are T sublists -- as the intermediate result list keeps getting longer, at each step a new intermediate result list object gets allocated, and all the items in the previous intermediate result must be copied over (as well as a few new ones added at the end). So, for simplicity and without actual loss of generality, say you have T sublists of k items each: the first k items are copied back and forth T-1 times, the second k items T-2 times, and so on; total number of copies is k times the sum of x for x from 1 to T excluded, i.e., k * (T**2)/2.

The list comprehension just generates one list, once, and copies each item over (from its original place of residence to the result list) also exactly once.

Related questions

0 votes
    Is there any shortcut to make a simple list out of a complex one in python? I already performed for a loop but ... help me with it. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    List the advantages NumPy Arrays have over (nested) Python lists?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    I have a series of lists, call them A, B, C, D, E. Now every list has 5 elements with ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    How will you compare two lists in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    E.x. listA = [9,10,11] listB = [12,13,14] Expected outcome: >>> concatenated list [9,10,11,12,13,14] Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    What Advantages Do Numpy Arrays Offer Over (nested) Python Lists?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    What is the difference between Python Arrays and lists?...
asked Dec 7, 2020 in Technology by JackTerrance
0 votes
    What is the difference between tuples and lists in Python?...
asked Nov 23, 2020 in Technology by JackTerrance
0 votes
    I have a list of files in a folder in my system file_list= ["A", "B", "C"] I Have read the files using ... C How do I accomplish this? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    The ADT list is more general than common lists and has entries that are objects of the same type. Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    How can we delete duplicate rows from flat files?...
asked Mar 26, 2021 in Technology by JackTerrance
0 votes
    How can we combine a database and the flat file data in Tableau Desktop?...
asked Oct 30, 2020 in Technology by JackTerrance
...