Filter function with Lambda and List in Python 3.X


#1

Hi everyone! I am not sure if this is an appropriate question for this forum, but I was trying to reproduce the Lambda Syntax lesson in Python 3 (using Jupyter iPython Notebooks) and the filter function does not seem to be working similarly to the way it works on Code Academy, which I know is using Python 2.X.

My question is, does anyone know how to reproduce the code/output with the syntax of Python 3? In Python 3 the same code that works on Code Academy gives me this output (I removed the actual solution so as not to break any rules):

languages = ["HTML", "JavaScript", "Python", "Ruby"]
print (filter(LAMBDA, LIST))

output: <filter object at 0x1040229b0>

Any advice would be appreciated as I would like to be able to work solely in Python 3.

Cheers,

Aaron


#2

That filter object is iterable, so you can for example create a list from it, or loop through it.

But I would use list comprehension instead of filter, does the same thing, easier to read imo


#3

languages = ["HTML", "JavaScript", "Python", "Ruby"]
print filter(lambda x: x, [languages[2]])

How about this? works!


#5

Hi aaronkub! i had the same problem, at really i’m doing the course here but i’m practicing in my computer with python 3 so i made this for solve the problem:

#Codigo para python2
garbled = “IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX”

message=filter(lambda x: x!=“X”,garbled)
print (message)

#Codigo para python 3
garbled = “IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX”

message=list(filter(lambda x: x!=“X”,garbled))
message=’’.join(message)
print (message)


#6

A post was split to a new topic: Filter in python3


#7