Map, Filter & Lambda Function Flashcards
A built in function in Python that allows you to process and transform all the items in an iterable w/o using an explicit for loop, a technique commonly known as (blank-ing)
map()
How do you map a function to a sequence?
def moneytone in seq
map(moneytone,seq)
How would you pass this map function into a list?
map(moneytone,seq)
list(map(moneytone,seq))
Consists of applying a predicate or Boolean-valued function to an iterable to generate a new iterable
Filtering
A lambda expression takes in a number and returns a condition T or F value based on the conditional operation
list(filter(lambda num: num*2 == 0,seq))
m = ‘ I Am The Greatest’
m.upper()
m.lower()
m.split()
What are these called
All uppercase
All lowercase
Splits a string on all the white spaces of that a string
Methods
How would you get the keys items and values of a dictionary?
m = {‘X1’ : 5, ‘X2’ : 8}
m.keys() [‘X1’ , ‘X2’]
m.items() [(‘X1’, 5), (‘X2’,8)])
m.values() [5,8]
Do dictionaries contain an order?
No
Use lambda exp and filter function to filter out the words from a list that don’t start with the letter ‘s’. For example:
www = [‘soup’, ‘dog’, ‘monkey, ‘dollar’, ‘salad’]
list(filter(lambda word: word[0]==‘s’ ,www))