+ 1

How do I use map and filter?

I've learned it just now but I don't think that I completely understand it. Can someone help me? Thanks

12th Feb 2022, 5:07 AM
FatFace0909
FatFace0909 - avatar
5 Respostas
+ 7
TD9 , here are 2 samples: 1. given from Cristian Baeza Jimenez , corrected to the required list name: oldList = [11, 12, 22, 33, 44, 55, 67] NewList= list(filter(lambda x: x%11==0, oldList)) # this is the correct list name print(NewList) # result: [11, 22, 33, 44, 55] 2. map() - sample with a list of strings, using a string method to make the first character of each element of the list as capital letter. this function is defined with map(), lst = ["tom", "ann", "bob", "sue"] res = list(map(str.capitalize, lst)) print(res) # result: ['Tom', 'Ann', 'Bob', 'Sue']
12th Feb 2022, 2:38 PM
Lothar
Lothar - avatar
+ 2
This is an example: oldList = [11, 12, 22, 33, 44, 55, 67] NewList= list(filter(lambda x: x%11==0, oldList))
12th Feb 2022, 6:08 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
+ 1
Cristian, what about map?
12th Feb 2022, 6:27 AM
FatFace0909
FatFace0909 - avatar
+ 1
Thanks Lothar , there was an error in my example.
12th Feb 2022, 2:46 PM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
0
Since others have given examples with code let me give oral example. Filter as the word implies means to filter a or some element(s) of interest from a particular list. For filter the elements must exist. The code helps to extract it from the list. For map as the word implies you want to create a new list from an existing one following a particular pattern. For map the list does not exist it has to be created from the universal list using the code
12th Feb 2022, 6:58 PM
okonkwo calistus
okonkwo calistus - avatar