14. Try It!


#1



I get what lambda is and what it does but when I try it on idle 3 it gives me a strange output. for example see code below.


I expected to see a list of [0,3,6,9,12,15]

Am I writing the wrong syntax for python3?


languages =["html", "java", "python", "ruby"]
filter (lambda x: x=="python", languages)
<filter object at 0x10558ed30>


#2

you need to use print:

print(filter (lambda x: x=="python", languages))

to print the result of the filter

Otherwise, it will just show you information about the filter object


#3

I did just now but result is still the same?

print (filter(lambda x: x=="python", languages))


#4

<filter object at 0x10558ee80


#5

it seems some changes where made to the behaviour of filter:


#6

In exercise 18. Lambda Expressions I have a similar problem. I pass the test on code academy with the following code:

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message=filter(lambda x: x!="X", garbled)
print message

and I get:
I am another secret message!

but when I write the same code on idle 3.x with list(filter), I am getting a list since I use list. Is there any other way to get the same output as in code academy?

my idle 3 code and the output:

>>> message_3=list(filter(lambda x: x!="X", garbled))
>>> print (message_3)
['I', ' ', 'a', 'm', ' ', 'a', 'n', 'o', 't', 'h', 'e', 'r', ' ', 's', 'e', 'c', 'r', 'e', 't', ' ', 'm', 'e', 's', 's', 'a', 'g', 'e', '!']

#7

you can also run your code in python2?

python3 made some changes, nothing we can do about this.

for your secret message, why use list(), why not str()?


#8

Nope, even with str() it gives me .

in python 2 (I mean codecademy exercise) it works just fine.

Any other bright ideas?


#10

this sucks, this works:

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message_3=filter(lambda x: x!="X", garbled)
print ("".join(list(message_3)))

but its not really nice

this is what the python docs say:

map() and filter() return iterators. If you really need a list, a quick fix is e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).

Weird.


#11

many thanks!!! Since you always provide very helpful comments do you mind looking at the following thread and advise me which interpretor to use?

sorry for the off topic help request. thx in advance!

httpx://discuss.codecademy.com/t/python-3-5-2/76838/5

Hello

I am learning python on codecademy. As additional testing I have idle 3.5.2 on my mac air and use the idle from time to time. however certain symbols e.g. ~ or ^ or even {} makes idle immediately crash on my mac.

Does any one have any idea why this is so?

dle is a python module, so you'd debug it the same way as any other python code

Since you ask, it's probably not something you want to dig into.

You're probably better off upgrading to a better editor anyhow, idle is kind of just for when the alternative is Notepad.exe

do you have any suggestions as a better editor? would be great to have one where I can also see the interpreter separately - just like in codecademy!


#12

i saw the topic, i don't have a clue what is causing the crash


#13

Could you please then suggest an interpreter to use? Which one are you using?


#14

i would suggest trying to figure out why python is crashing


#15