Lambda, Map, Reduce, Filter, and other Special Function Flashcards

(22 cards)

1
Q

____ Functions are anonymous functions means that the function is without a name.

A

lambda

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

s1 = ‘GeeksforGeeks’

s2 = lambda func: func.upper()
print(s2(s1))

A

GEEKSFORGEEKS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Python lambda function syntax

A

Syntax: lambda arguments : expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Example: Check if a number is positive, negative, or zero
n = lambda x: “Positive” if x > 0 else “Negative” if x < 0 else “Zero”

print(n(5))
print(n(-3))
print(n(0))

A

Positive
Negative
Zero

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Lambda with List Comprehension
li = [lambda arg=x: arg * 10 for x in range(1, 5)]
for i in li:
print(i())

A

10
20
30
40

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example: Check if a number is even or odd
check = lambda x: “Even” if x % 2 == 0 else “Odd”

print(check(4))
print(check(7))

A

Even
Odd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Lambda Function with Multiple Statements

A

Example: Perform addition and multiplication in a single line
calc = lambda x, y: (x + y, x * y)

res = calc(3, 4)
print(res)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The _____ function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a ____ object (which is an iterator).

A

map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

s = [‘1’, ‘2’, ‘3’, ‘4’]
res = map(int, s)
print(list(res))

A

[1, 2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Syntax for map function

A

map(function, iterable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

a = [1, 2, 3, 4]

Using custom function in “function” parameter
# This function is simply doubles the provided number
def double(val):
return val*2

res = list(map(double, a))
print(res)

A

[2, 4, 6, 8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

a = [1, 2, 3, 4]

Using lambda function in “function” parameter
# to double each number in the list
res = list(map(lambda x: x * 2, a))
print(res)

A

[2, 4, 6, 8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Map with multiple iterables

a = [1, 2, 3]
b = [4, 5, 6]
res = map(lambda x, y: x + y, a, b)
print(list(res))

A

[5, 7, 9]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

words = [‘apple’, ‘banana’, ‘cherry’]
res = map(lambda s: s[0], words)
print(list(res))

A

[‘a’, ‘b’, ‘c’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

s = [’ hello ‘, ‘ world ‘, ‘ python ‘]
res = map(str.strip, s)
print(list(res))

A

[‘hello’, ‘world’, ‘python’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The ______ method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

17
Q

The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

18
Q

syntax for filter() function

A

Syntax: filter(function, sequence)

19
Q

function: A for filter

A

function that defines the condition to filter the elements. This function should return True for items you want to keep and False for those you want to exclude.

20
Q

iterable for filter

A

The iterable you want to filter (e.g., list, tuple, set

21
Q

a = [1, 2, 3, 4, 5, 6]
b = filter(lambda x: x % 2 == 0, a)

print(list(b))

22
Q

Combining filter and map

a = [1, 2, 3, 4, 5, 6]

First, filter even numbers
b = filter(lambda x: x % 2 == 0, a)

Then, double the filtered numbers
c = map(lambda x: x * 2, b)

print(list(c))