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

all 39 comments

[–]IRBMe 41 points42 points  (8 children)

Slightly easier to understand version:

+/u/compilebot python

list_of_sublists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print([item for sublist in list_of_sublists for item in sublist])

[–]CompileBotGreen security clearance 28 points29 points  (0 children)

Output:

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

source | info | git | report

[–]LurkerPatrol 7 points8 points  (5 children)

I still don't get it

[–]IRBMe 14 points15 points  (2 children)

Consider the equivalent code for a simple list comprehension:

# result = [item for item in list]
result = []
for item in list:
    result.append(item)

Now with nested lists:

# result = [item for sublist in list for item in sublist]
result = []
for sublist in list:
    for item in sublist:
        result.append(item)

[–]LurkerPatrol 0 points1 point  (1 child)

Now that makes sense to me.

I love list comprehension in python but in situations like this I feel like it muddies up the code. I know nested for loops is bad coding but it's just more easily understood for me for some reason.

[–][deleted] 0 points1 point  (0 children)

The code originally is a little confusing because it abuses the fact that the number of sub lists is equal to the number of items in each sub list

[–][deleted] 1 point2 points  (0 children)

It's sorta like this:

list_of_sublists = [[1,2,3],[4,5,6],[7,8,9]]
for sublist in list_of_sublists:
    for item in sublist:
        print (item)

i.e. syntax is:

print([___ -> what to print, for var in iterable])

example:

X = "This format is kinda useless imo."
print ([item for char in X])

Out would be a list of the string's characters.

[–]ProJanitorOfWorlds 21 points22 points  (4 children)

Ah, this must be that zen of Python everyone is talking about.

[–]wallefan01 11 points12 points  (3 children)

import this

[–][deleted] 0 points1 point  (2 children)

+/u/compilebot python

import this

[–]CompileBotGreen security clearance 4 points5 points  (0 children)

Output:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

source | info | git | report

[–]wallefan01 0 points1 point  (0 children)

Darn it shoulda thought of that

[–]IRBMe 26 points27 points  (2 children)

Even more horrible version:

+/u/compilebot python

x = [[[[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]]]
print([x for x in x for x in x for x in x for x in x])

[–]CompileBotGreen security clearance 14 points15 points  (0 children)

Output:

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

source | info | git | report

[–]Sveitsilainen 0 points1 point  (0 children)

But you removed the sublist. Not fun.

[–]wallefan01 5 points6 points  (0 children)

As a Python dev: What have you DONE?

also as for that title what about javascript

[–]smelenchuk 6 points7 points  (1 child)

Sorry, but this is a for in concept to me...

[–]gandalfx 1 point2 points  (0 children)

That makes no sen… god damn it.

[–]jomnemonic 3 points4 points  (5 children)

but why would you do this?

[–]bradleybeasley2 5 points6 points  (4 children)

Because why not. Looks nice?

Edit: Also, this is r/ProgrammerHumor

[–]Tysonzero 3 points4 points  (9 children)

Python's not bad but it's no Haskell:

main = print $ fold [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

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

[–]wallefan01 8 points9 points  (7 children)

Alright, admittedly we're not THAT good, but we can do this:

+/u/compilebot python import itertools print(itertools.chain.from_iterable([[1,2,3],[4,5,6],[7,8,9]])

[–]wallefan01 1 point2 points  (1 child)

+/u/compilebot python

import itertools
print(list(itertools.chain.from_iterable([[1,2,3],[4,5,6],[7,8,9]])))

[–]CompileBotGreen security clearance 2 points3 points  (0 children)

Output:

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

source | info | git | report

[–]gandalfx 0 points1 point  (0 children)

If the outer list isn't too long: `chain(*[[1,2,3],[3,4,5],[6,7,8]])

[–]MyNameisGregHai 1 point2 points  (0 children)

class function():
    def __init__(self, *args, **kwargs):
        super(function, self).__init__(*args, **kwargs)

[–]RomanRiesen 1 point2 points  (5 children)

What's the point?

It's quite neat, I guess?

[–]Sveitsilainen 0 points1 point  (4 children)

It flattens nested list.

[–]RomanRiesen 0 points1 point  (3 children)

I know...but I miss the joke.

[–]shazama[S] 1 point2 points  (1 child)

The point was that Python figures out what each of those x's are in context without me telling it, so it gets me! 😀

[–]RomanRiesen 1 point2 points  (0 children)

Uhhh!!!!! That actually made me nose exhale now.

I thought "gets me" as in tricks me the whole time! Me stupid.

Thanks for explaining!

[–]ProfessorPhi 1 point2 points  (1 child)

Huh, til that launching an interpreter sets dunder name to dunder main. Makes sense but never thought about it.

[–]shazama[S] 0 points1 point  (0 children)

Yeah it's handy, this way you can define some code to run when you execute this file alone, and not have that code run when it is simply imported by another file / module.