1

When reading the official tutorial, I encountered this example:

>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

I couldn't understand this, so I did some experiments:

>>> [num for elem in vec]
[9, 9, 9]
>>> [num for elem in (vec for num in elem)]
[9, 9, 9]

And I'm even more confused now!

So, my question is: In which order should I read a list comprehension?


Edit: I'm sure I haven't defined a num variable with the value 9 anywhere.

sunqingyaos-MacBook-Air:Documents sunqingyao$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> num
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'num' is not defined
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [num for elem in vec]
[9, 9, 9]
>>> [num for elem in (vec for num in elem)]
[9, 9, 9]
>>> 
nalzok
  • 13,395
  • 18
  • 64
  • 118
  • 1
    I'm confused too. `[num for elem in vec]` should throw an error because `num` is not defined. Are you sure you haven't defined a `num` variable with the value `9` somewhere? – Aran-Fey Jul 16 '16 at 07:44
  • @Rawing Yes, I'm sure. See my updated question – nalzok Jul 16 '16 at 07:48

4 Answers4

5

The loops in list comprehension are read from left to right. If your list comprehension would be written as ordinary loop it would look something like this:

>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> l = []
>>> for elem in vec:
...     for num in elem:
...         l.append(num)
...
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In Python 2 the variables within the list comprehension share the outer scope so num is available to be used later:

>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> num
9

Note that on Python 3 behavior is different:

>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> num
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'num' is not defined
niemmi
  • 16,668
  • 7
  • 33
  • 40
2

No worries :).

When you execute the list comprehension the value of num is 9 so the next time you iterate through the vec. You will get a list of 9.

See this.

In [1]: vec = [[1,2,3], [4,5,6], [7,8,9]]
In [2]: [num for elem in vec for num in elem]
Out[2]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]: num
Out[3]: 9
In [4]: [num for elem in vec]
Out[4]: [9, 9, 9]
Rahul K P
  • 10,793
  • 3
  • 32
  • 47
  • 2
    @sunqingyao No, it's not a bug that list comprehensions in older versions of Python run in the scope of the surrounding code. It was intentionally done like that for reasons of efficiency. But so many complained about it that it was changed for Python 3. Which means that Python 3 list comps don't "leak", but they are also a little slower than Python 2 list comps, and consume a little more RAM. – PM 2Ring Jul 16 '16 at 08:45
0
list1 = [num for elem in vec for num in elem]

#is equivalent to:

list1 = []
for elem in vec:
    for num in elem:
       list1.append(num)
Maverick Meerkat
  • 4,814
  • 2
  • 40
  • 59
0

Let me try to make the answer more clears. And the order obviously will be from left to right and most right value will be stored in the variable i.e num and elem.

Initial Data:

vec = [[1,2,3], [4,5,6], [7,8,9]]
num  # Undefined
elem # Undefined

Step-1: After execution of line [num for elem in vec for num in elem]

# Now the value of undefined variable will be
num = 9  # will keep the last value of the loop as per python 2.7
elem = [7, 8, 9]   # Same applies here (as the loop for elem in vec will get executed first followed by for num in elem)

Step-2: After execution of line [num for elem in vec]

# Output: [9, 9, 9]
# Since num value is 9 and its get repeated 3 times because of vec has 3 elements (three list object in a list, so for loop will run 3 times)
# Now the variable value would be
num = 9 # no change
elem = [7, 8, 9] # The last tuple of variable vec

Step-3: After the execution of [num for elem in (vec for num in elem)]

1. In the first/right loop i.e (vec for num in elem)
    Here the result will be a generator that will have run thrice since length of elem is 3.
2. Final for loop will iterate over RESULT1 (the result of for loop #1 having length 3) and since the num value is 9. the result will be [9, 9, 9] # num value repeated thrice.
MaNKuR
  • 2,362
  • 1
  • 18
  • 29