Filter function with Lambda and List in Python 3.X


#1
languages = ["HTML", "JavaScript", "Python", "Ruby"]

# Add arguments to the filter()
print filter(lambda x: x == "Python", languages)

Shouldn’t this code return a boolean value rather than the string “Python”?


#2

this depends on the python version, in python2, a list is returned

in python3, a filter object is returned. This is more efficient, if you then need a list you can use list() to convert filter object to list


#3

Looks as though thee did not savvy, sire! Compile this piece of code of a computer and thy output shall speak to you. Hit me back soon after, squire!


#4

we can’t compile python code, python code is interpreted at runtime.

I ran the code, and produces a list, the desired behaviour of filter() in python2, as you can verify in the documentation.


#5

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