Lambda Flashcards
print((lambda a: a+10)(5))
15
print((lambda a,b:a*b)(5,6))
30
x = lambda a, b, c: a + b + c
print(x(5, 6, 2))
13
print(list(map(lambda x: x.capitalize(), [‘cat’, ‘dog’, ‘cow’])))
[‘Cat’, ‘Dog’, ‘Cow’]
print(list(map(lambda x: x[::-1], [‘cat’, ‘dog’, ‘cow’])))
[‘tac’, ‘god’, ‘woc’]
print(list(map(lambda x: x.replace(‘o’,’a’), [‘cat’, ‘dog’, ‘cow’])))
[‘cat’, ‘dag’, ‘caw’]
#There are spaces to the left and right of cat, dog, and cow print(list(map(lambda x: x.strip(), [' cat ', ' dog ', ' cow '])))
[‘cat’, ‘dog’, ‘cow’]
print(list(map(lambda x: x.endswith(‘g’), [‘cat’, ‘dog’, ‘cow’])))
[False, True, False]
print(list(map(lambda x: x.isalpha(), [‘cat’, ‘dog’, ‘cow’])))
[True, True, True]
print(list(map(lambda x: x.count(‘o’), [‘cat’, ‘dog’, ‘cow’])))
[0, 1, 1]
print(list(map(lambda x: len(x), (‘cat’, ‘banana’, ‘cherry’))))
[3, 6, 6]
avar = lambda x: x != 2 and x != 4
print(list(filter(avar, range(6))))
[0, 1, 3, 5]
avar = lambda x: x < 4 or x > 8
print(list(filter(avar, range(11))))
[0, 1, 2, 3, 9, 10]
print(list(filter(lambda x: x%2 == 0, range(11))))
[0, 2, 4, 6, 8, 10]
student_tuples = [(‘john’, ‘A’, 15),(‘jane’, ‘B’, 12),(‘dave’, ‘B’, 10),]
print(sorted(student_tuples, key=lambda student: student[2]))
#sorts 10, 12, then 15 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]