(I Hope this is not a duplicate, but other questions are too many and badly named to find if so...)
(Tested in Python > 3, Solution - if any - must work in >= 2.7)
Assuming we have a list:
l = [[1,2], [3,4], [5,6]]
We attempt to flatten it using the standard list comprehension method:
[v for sl in l for v in sl] == [1,2,3,4,5,6]
Output:
True
We then attempt:
[(v, s1[0]) for sl in l for v in sl]
Which causes:
NameError: name 's1' is not defined
Why does this happen?
Isn't s1
supposed to be accessible given that the for loops are evaluated as the loop should be evaluated as:
[<expression> for <outer> in <list> for <inner> in <outer>]
Or in other words:
r = []
for <outer> in <list>:
for <inner> in <outer>:
r.append(<inner>)
What am I missing?
It appears you are mixing up the names sl
("ess-ell") vs s1
("ess-one").
As an addendum, you should consider switching to a programmer-specific font which emphasizes the differences between otherwise potentially ambiguous characters. One popular option is Source Code Pro.