Skip to content

Instantly share code, notes, and snippets.

@zelark
Last active August 29, 2015 13:57
Show Gist options
  • Save zelark/9763145 to your computer and use it in GitHub Desktop.
Save zelark/9763145 to your computer and use it in GitHub Desktop.
The same variable through a list comprehension.
def foo():
x0 = [[1, 2], [3, 4], [5, 6]]
y = [x2 for x1 in x0 for x2 in x1]
print y
def bar():
x = [[1, 2], [3, 4], [5, 6]]
x = [x for x in x for x in x]
print x
>>> def foo():
... x0 = [[1, 2], [3, 4], [5, 6]]
... y = [x2 for x1 in x0 for x2 in x1]
... print y
...
>>> def bar():
... x = [[1, 2], [3, 4], [5, 6]]
... x = [x for x in x for x in x]
... print x
...
>>> foo()
[1, 2, 3, 4, 5, 6]
>>> bar()
[1, 2, 3, 4, 5, 6]
>>>
>>> import dis
>>> dis.dis(foo.__code__)
2 0 LOAD_CONST 1 (1)
3 LOAD_CONST 2 (2)
6 BUILD_LIST 2
9 LOAD_CONST 3 (3)
12 LOAD_CONST 4 (4)
15 BUILD_LIST 2
18 LOAD_CONST 5 (5)
21 LOAD_CONST 6 (6)
24 BUILD_LIST 2
27 BUILD_LIST 3
30 STORE_FAST 0 (x0)
3 33 BUILD_LIST 0
36 LOAD_FAST 0 (x0)
39 GET_ITER
>> 40 FOR_ITER 25 (to 68)
43 STORE_FAST 1 (x1)
46 LOAD_FAST 1 (x1)
49 GET_ITER
>> 50 FOR_ITER 12 (to 65)
53 STORE_FAST 2 (x2)
56 LOAD_FAST 2 (x2)
59 LIST_APPEND 3
62 JUMP_ABSOLUTE 50
>> 65 JUMP_ABSOLUTE 40
>> 68 STORE_FAST 3 (y)
4 71 LOAD_FAST 3 (y)
74 PRINT_ITEM
75 PRINT_NEWLINE
76 LOAD_CONST 0 (None)
79 RETURN_VALUE
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment