Am trying lambda function in pycharm and for some reason it does not run as it does in editor of Codecademy


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

I am trying lambda function in pycharm and for some reason it does not run as it does in editor of code academy. I heard about python 2 vs 3 being used. I just wanna test things learned in code academy in different editors. the error is as follows: basically it doesn’t do anything. thanks!

<filter object at 0x100741780>

list = range(20)

print (filter(lambda x: x % 2== 0, list))

As I understand it (barely) unlike Python 2, which returns a list, Python 3 does not. We need to invoke the list function on the returned object.

rge = range(21)
is_even = lambda x: x % 2 == 0
evens = list(filter(is_even, rge))
print evens

# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
thank you!
1 Like

As a sort of supplement but with code that is highly improbable in its present form except for demonstration and study, here is an implementation of the above…

def fn1():
    return lambda x: x % 2 == 0
def fn2(is_even, r):
    return list(filter(is_even, range(r)))
def fn3(evens):
    return list(map(lambda x: x + 1, evens))

def main():
    r = 21
    s = fn1()
    t = fn2(s,r)
    u = fn3(t)

    while True:
        done = 0
        for b in u,s,t:
                done += 1
        if done == 3:
    print (t)
    print (u)


[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]

No, I did not conceive of the loop(s) although I certainly recognized the potential on first glance at the code I stole them from, The forest example from the Turtle samples in IDLE. I just thought it was so cool that we could iterate through a list of variables that are themselves tied to one or multiple functions. It bears further study, that’s for sure. But, don’t let it scare you or interfere with your track studies here. This is just a sidebar.

One more question. I tried something new. But i guess I don’t understand what “invoke” means. can you elaborate for the following example that didn’t go through. Sorry! Thanks again!

I am just concerned that after code academy i still need to learn new ways to be able to work on python 3 :frowning:

filter object at 0x1019cfb38
message = filter(lambda x:  x != "X", garbled)
print (message)

Invoke is just another word for calling.

You’re always going to have to learn more, and you can’t know it all. So really you should get used to the idea that what you’re really only ever learning is overall concepts, making observations, looking up and applying details.

The biggest difference in Python3 is handling unicode, something you probably haven’t touched on at all. Everything else is really just something you have to take in your stride.

The same concepts apply to both languages, but they may use them differently in a few places.

For example, that filter object: What that is, is an object that knows how to filter through your other iterable. It hasn’t done it yet but will do it when you request values from it. This is called lazy evaluation and the opposite is called eager evaluation. Objects like that exist in both versions, it’s just that a few things are using them instead of lists.

1 Like

Also, I have nothing kind to say about IDE’s, they hide what’s going on and you learn the IDE instead of the tools you’re really using. And when things go wrong you might not know how to fix it - when it blows up the first thing to do is close the IDE and pull up a terminal and use the real tools.

You’re free to prefer an IDE, just keep in mind that in no way is your IDE the universe. Java and C# are “IDE languages” with highly integrated IDE’s which also work quite well. Python doesn’t really give an IDE much to work with (because of type-less variables (values know their types, variables are just names) so I find Python-IDE’s a whole lot more questionable.

Python is very self-aware (reflective) so I think it makes more sense to interact with Python directly than to use an IDE. (Run the code and drop into interactive mode and then poke around while it’s running)

1 Like

Per the earlier example,

message = list( ... )
1 Like