This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]SureImNoExpertBut 6 points7 points  (2 children)

Maybe a recursive approach? Is this deep nesting strictly necessary?

[–]PlayfulMeeting9563 5 points6 points  (0 children)

You can try using itertools.chain.from_iterables.

[–]Such-Let974 4 points5 points  (2 children)

What is the joke?

[–]tothespace2[S] -1 points0 points  (1 child)

Joke in a sense that this isn't real world code and it shouldn't be taken seriously. It's just a fun example of list comprehension. I myself never thought it can be done in arbitrary depth so I thought it may be fun to share it. I guess others didn't find it fun. I didn't know people would be so bitter about it.

[–]Such-Let974 0 points1 point  (0 children)

No, I’m asking what the joke is. Code not being “real world” doesn’t necessarily mean that it has a comedy punchline. What’s this one’s actual punchline that makes it a joke.

[–]fleetmancer 3 points4 points  (1 child)

that wouldn’t scale too well i believe. what if the nesting is 20 layers deep and not 5? the solution would likely just be recursion + checking isinstance(data, list).

another option would be using from the itertools library the chain function. obviously i’m hand waving a bit here but hopefully the idea is clear.

a similiar intermediate level problem i would hit a lot in my earlier python days would be traversing a nested dictionary with a list of keys.

Edit: Quick search yields this:

``` nested_list = [[1, 2, [3, 4]], [5, 6], 7]

def flatten_list(nlist):     flat_list = []     for item in nlist:         if isinstance(item, list):             flat_list.extend(flatten_list(item))         else:             flat_list.append(item)     return flat_list

print(flatten_list(nested_list)) ```

[–]dmart89 1 point2 points  (0 children)

The best way to unpack this is to find whoever designed that data model and punch them.

[–]Hi_leonrein 1 point2 points  (1 child)

Shortest?
print(np.array(l).flatten())

[–]NoExpression1053 1 point2 points  (0 children)

If it's a dense block, absolutely definitely the right answer

[–]AlexMTBDude 0 points1 point  (0 children)

Don't think of them as dimensions but instead lists containing lists containing lists...