0

I am getting an unexpected output when I execute the following python code. Iam new at python.

def foo(n):
    return n+2;
print (filter(foo,[1,2]))

i was expecting output as :-

[3,4]

but Iam getting output as:-

[1,2] 

please help me solve this.

1
  • 3
    Wrong function. You want map, not filter. Commented Jan 10, 2018 at 5:16

1 Answer 1

2

What you described is map and not filter.

def foo(n):
    return n+2;
print (map(foo,[1,2]))

Prints out

[3,4]
Sign up to request clarification or add additional context in comments.

3 Comments

so you mean to say filter returns the original values after checking if it is true or not??
filter retains the values in the list that pass foo. e.g. def foo(n): return n>=2 print(filter(foo,[1,2])) will give [2].
...i got it. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.