In Python, a map() refers to the process of applying a function to each item in an iterable (like a list, tuple, or dictionary) and returning a new iterable with the results. map() function takes two arguments: a function and an iterable. The function is applied to each item in the iterable, and the results are returned as a new iterable. These terminologies might sound a bit unfamiliar so let’s explain with some examples
syntax:
Assuming we have a string “1/2″ from a user’s input and we want to perform an arithmetic operation on this string, how might we go about that, well lets list out the steps
Traditional approach
Step 1
Split the fraction, using the split() method and “/” as it’s delimiter
Step 2
The split function returns a list of two values(strings) which we can assign to an unpacked variable a and b
Step 3
Convert a and b to an int
b = int(b)
Step 4
Finally we perform our arithmetic operation with
print(answer)
Putting everything together we have 5 lines of code
a = int(a)
b = int(b)
fraction = a / b
print(fraction)
Output: 0.5
Using map() function
Now, lets see how we can use the map function to achieve same result but with fewer lines of code
Step 1
Split the fraction, using the split() method “/” as it’s delimiter
Step 2
The split function returns a list of two values(strings) which we can assign to an unpack value
Step 3
Use the map() function to convert the return values of the split() to an int
Step 4
Finally we perform our arithmetic operation with
Putting all together we have 3 lines of code as to 5 lines of code from the traditional method
answer = a / b
print(answer)
Output:0.5
Note that the split() function returns a list of strings [“1”, “2”] and key word int is a python function which is applied to each item in the list and these values are unpacked and assigned to the variable a and b
Map() and Lambda, What is a Lambda function ?
A lambda function is an anonymous function defined using the lambda keyword. It is a small, inline function that can have any number of arguments but can only have one expression.
Syntax:
Example
Assuming we need a function that adds two numbers together, let us see how we can achieve this using both the traditional way and the lambda function
Traditional way
result = a + b
return result
and to call this function in the main function will be like this
Output:7
Using a lambda function
and we call the function in the main function, same way
Output: 15
As seen from the example above a lambda function can take any number of arguments
Now lets see an example as to how we can use both a map() and lambda function
assuming we want a function that returns a list but the first character of each word of this list is being capitalized, say we want to convert a list like this [“serah”, “hakim”, “adam”, “kwame”] to this [“Serah”, “Hakim”, “Adam”, “Kwame”]
Traditional approach
new_word = []
for word in word_list:
new_word.append(word.title())
return new_word
print(title_each_word([“serah“, “simi“, “nana“]))
Output: [“Serah”, “Hakim”, “Adam”, “Kwame”]
Using lambda and map() function
new_word = map(lambda word: word.title(), word)
print(list(new_word))
Output: [“Serah”, “Hakim”, “Adam”, “Kwame”]
You can see how concise the map and lambda function have made our code look like, now when does one use a lambda function or a map function
map()
lambda
It is used when you want to apply a single transformation function to all the iterable elements and return an iterator
A quick function you can just use and throw away without a name or sort
Conclusion
Just like any concepts, with regular practice these Python features will become second nature if you keep at it as familiarizing yourself with them will ensure you adhere strictly to the DRY principles