Please help me understand what filter() is exactly doing here. | SoloLearn: Learn to code for FREE!

+9

Please help me understand what filter() is exactly doing here.

# What is the output of this code? A={1,2,3,5,3,2,1} print(sum(((list(filter(lambda x:x%2==0,A)))))) Output: 2 https://code.sololearn.com/c7oBy05aubsf/?ref=app

10/20/2020 3:06:55 AM

Prashanthi Reddy

11 Answers

New Answer

+17

Prashanthi Reddy No, you are totally okay. You used a set as your iterable object, and even though you inserted the number 2 two times in your set, your set will remove all duplicates automatically. That's the normal behaviour of the datatype 'set' in Python. So there is only one number 2 in your set. If you used a list, at which you would have to choose square brackets [...] instead of the curly ones {...}, then you would have gotten your anticipated result of 4. Lists work different than sets.

+15

filter() fetches all these values from the set A which meet the condition, stated within the lambda function. The lambda function asks for a number modulo 2 equals 0, so it asks for even values.

+8

The variable A is a set and all duplicate values within are omitted. So, A = {1,2,3,5} # the extra 3,2,1 are not added. The filter built-in function takes 2 arguments. The first is the function that will be used to keep values in the returned filter object. This function will return True or False. A True value returned will keep the item from the passed in iterable in the returned filter object. False will 'filter' the item out. The second argument passed to the filter function is the iterable to be filtered. https://docs.python.org/3/library/functions.html#filter Also, note that a lambda or inline function is being used here. This function takes an item 'x' from the iterable and returns True if the item is even (divisible by 2) using the modulo operator 'x % 2 == 0'. So, the only remaining item in the filter object is 2 which is then converted to a list() = [2]

+7

Excellent answers in this thread so far. Prashanthi Reddy It might help if you experiment with your code. Try replacing an odd number with an even number not already in the set. Use 6 for example. The result should be 8 (2+6). Add another 6 and it will still result in 8 because duplicates are prevented by the set data type. Or try changing the lambda so it returns a boolean using another expression criteria. Try evaluating for multples of 3, 4, or any odd number. Try it without the set to see the behavior with duplicates. This will help move your understanding forward.

+6

Prashanthi Reddy, no if you read my answer you would see that I explained that A is a set, so the duplicates are omitted. This happens upon creation in this case so the duplicates won't be there when A is passed to the filter function. You can add a print(A) statement prior to the last line and after the declaration of A to see what A contains.

+5

Thank you Jan Markus 😊 I get it

+4

David Carroll yea..trying different numbers helped me understand some other possibilities too. thanks

+4

ChaoticDawg yes thank u, but it was my mistake that I thought the list() function in the print would make it different to what u said.

+3

Jan Markus then the filter() will fetch 2, 2(bcuz there are two even numbers). Sum([2,2]) should be 4 ryt Sry if u feel I am dumb asking this😅 I am really confused

+3

A={1,2,3,5,3,2,1} print(sum(((list(filter(lambda x:x%2==0,A)))))) Actually since a is a set , it is interpreted by python as A = {1,2,3,5} NOW , if you dont know what a set is , it is a datatype just like a list or tuple which ONLY CONTAINS UNIQUE VALUES . which means that all the repetitions will be removed . This is why , A = {1,2,3,5} #The 3,2,1 in the end were removed because it is already in the set. Now , in the set {1,2,3,5} , only 2 is an even number , so sum(2) will give 2 . If you still didnt understand try learning more about sets . https://www.sololearn.com/learn/Python/2464/ Hope it helps

+1

The function filter filters an iterable by removing items that don't match a predicate (a function that returns a Boolean). your function filters the elements which are divided by 2 and have no remainders.so after filter it will be 2