Considering the code below:
class Test:
l = [1, 2, 3]
foo = lambda x: x
for x in l:
print(foo(x))
[print(foo(x)) for x in l]
if __name__ == '__main__':
test = Test()
The output is the following:
1
2
3
... in <listcomp>
[print(foo(x)) for x in l]
NameError: name 'foo' is not defined
I don't understand why isn't foo
a visible function from within the list iteration. Probably something to do with scopes but I am looking for the right explanation, with documentation to support it if possible.
ps: I am not interested in alternative implementations for the sake of just fixing the code.
The for loop is a common way to iterate through a list. List comprehension, on the other hand, is a more efficient way to iterate through a list because it requires fewer lines of code. List comprehension requires less computation power than a for loop because it takes up less space and code.
Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.
List comprehensions are faster than loops in general. It achieves to be faster by loading the entire list into the memory.
List comprehensions are also more declarative than loops, which means they're easier to read and understand. Loops require you to focus on how the list is created. You have to manually create an empty list, loop over the elements, and add each of them to the end of the list.
As we can see, the for loop is slower than the list comprehension (9.9 seconds vs. 8.2 seconds). List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration. This is slow. Side note: It would even be worse if it was a Numpy Array and not a list.
1 Comprehension of the list is an effective means of describing and constructing lists based on current lists. 2 Generally, list comprehension is more lightweight and simpler than standard list formation functions and loops. 3 We should not write long codes for list comprehensions in order to ensure user-friendly code. More items...
Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops. Below is the program which implements nested loop:
Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops. Now by using nested list comprehensions same output can be generated in fewer lines of code. Lambda Expressions are nothing but shorthand representation of Python functions.
As suggested in the comments, this question seems to pretty much answer the issue and goes a long way to thoroughly explain it. There is however a small variation that can be solved following a similar approach. The resulting code is as follows:
class Test:
l = [1, 2, 3]
foo = lambda x: x
(lambda foo=foo, l=l: [print(foo(x)) for x in l])()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With