12. Anonymous Functions - Python 3 ... * <-- what is that


I have been using a Python 3 environment to test and store code as I go. I have been able to work through the differences in Python 2 vs Python 3 pretty well until this chapter. I have found the ‘*’ helpful to allow me to use some of the code, but I don’t know why. Can anyone explain this for me, please.

Example working in Codecademy :

my_list = range(16)
print filter(lambda x: x % 3 == 0, my_list)

...and from earlier in Advanced Topics...

new_list = range(101) 

These work in Python 3+ as:

my_list = range(16)
print(*filter(lambda x: x % 3 == 0, my_list))

new_list = [*range(101)] 


i found this beautiful answer:


its long, but it does a really good job of in detail explaining this.

in python2, range and filter return a list. In python3, they return iterators. Iterators hand you the values one by one, this is a performance improvement. Of course, if you need the values in a list, you could unpack (*) or just use list() if you don’t want to confuse yourself


Thank you so much. Great job simplifying that and making it easy to understand.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.