-3

I've come across this single line code, which is a function definition, that flattens a list of lists to produce a single list. Can someone explain to me, term by term, what does this mean? How does it work?

lambda l : [item for sublist in l for item in sublist]
navajyothmp
  • 85
  • 1
  • 11
  • [List comprehension explained](http://stackoverflow.com/questions/20639180/python-list-comprehension-explained). – xyres Apr 30 '17 at 15:13
  • 1
    The syntax is wrong, it must have been `[item for sublist in l for item in sublist]` – Thierry Lathuille Apr 30 '17 at 15:15
  • Actually it's just a `SyntaxError`... but please explain what you don't understand. The `lambda`? The list-comprehension? Loops? Variables? – MSeifert Apr 30 '17 at 15:15
  • 1
    This seems to be a classical overuse of lambdas. It can just be a normal function. – Grimmy Apr 30 '17 at 15:20

2 Answers2

2

Sadly as far as I know there's no way to really blindly unpack an indefinitely nested list in python.

Op appears to have attempted to copy from

Making a flat list out of list of lists in Python

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

I tested the above on python 3.6, four-nested structure. Sadly it only unpacks the outer layer. I need to use it in a loop three times to fully unpack the structure.

import numpy as np
x = np.arange(625).reshape(5,5,5,-1).tolist() #4-nested structure
flatten = lambda x: [item for sublist in x for item in sublist]
y = flatten(x) #results in 3-nested structure of length 25.

The same problem also exists for the more versatile below function (which requires an import):

from itertools import chain
y = list(chain.from_iterable(x)) #as per flatten() above, unpacks one level

For multiple layers and if you're not too concerned about overhead, you could just do the following:

import numpy as np
y = np.array(x).flatten().tolist() #where x is any list / tuple / numpy array / 
                          #iterable

Hope the above helps :)

p.s. Ahsanul Hafique's unpacking illustrates the logic of the lambda function as requested by op. I don't believe in lambda functions and you just have to look at Ahsanul's unpacking to see why. It would be trivial to factor out the unpacking , while in the main function check if the sublist is a list or list element, and unpack or append as appropriate and thus create a completely versatile list unpacker using two functions.

Community
  • 1
  • 1
1

It means converting a 2D list into a 1D list i.e a flat list.

For example if you have a list in the form:

lst = [[1,2,3], [4,5,6], [7,8,9]] 

The output you wanted is:

lst = [1,2,3,4,5,6,7,8,9]] 

Let's see the function definition:

lambda l : [item for sublist in l for item in sublist]

Which equivalents:

def flatten(l):
    result = []
    for sublist in l:    # here shublist is one of the innerlists in each iteration 
        for item in sublist: # One item of a particular inner list 
            result.append(item) #Appending the item to a flat list.
    return result
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57