+ 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
5 Answers
+ 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']
+ 2
This is an example: oldList = [11, 12, 22, 33, 44, 55, 67]
NewList= list(filter(lambda x: x%11==0, oldList))
+ 1
Cristian, what about map?
+ 1
Thanks Lothar , there was an error in my example.
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