Getting Started with Python's map() Function: A Beginner's Guide
Tags: python, programming, beginners, developer
Published: 2024-03-26 00:31:26

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:` ```python
map(function, iterables)
```
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
```python
“1/2".split(“/”)
```
**Step 2**
The split function returns a list of two values(strings) which we can assign to an unpacked variable `a` and `b`
```python
a, b = “1/2".split(“/”)
```
**Step 3**
Convert a and b to an int
```python
a = int(a)
b = int(b)
```
**Step 4**
Finally we perform our arithmetic operation with
```python
answer = a / b
print(answer)
```
Putting everything together we have 5 lines of code
```python
a, b = "1/2".split("/")
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 ```python
“1/2".split(“/”)
```
**Step 2**
The split function returns a list of two values(strings) which we can assign to an unpack value
```python
a, b = “1/2".split(“/”)
```
**Step 3**
Use the `map()` function to convert the return values of the `split()` to an int
```python
a, b = map(int, “1/2".split(“/”))
```
**Step 4**
Finally we perform our arithmetic operation with
```python
answer = a / b
```
Putting all together we have 3 lines of code as to 5 lines of code from the traditional method
```python
a, b = map(int, "1/2".split("/"))
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:` ```python
lambda arguments: expression
```
**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**
```python
def add(a,b):
    result = a + b
    return result
```
and to call this function in the main function will be like this
```python
print(add(4,3))
```
`Output:7`
**Using a lambda function**
```python
add = lambda a,b: a + b
```
and we call the function in the main function, same way
```python
print(add(6,9)
```
`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**
```python
def title_each_word(word_list):
   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**
```python
word = ["serah", "hakim", "adam", "kwame"]
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