map

The `map` function in Python allows you to apply a given function to all items in an iterable (such as lists or tuples) and return a new iterable with the transformed data. This can be particularly useful in data science for preprocessing and transforming data efficiently. In this tutorial, we'll explore how to use the `map` function through various practical examples.

### Basic Usage of the `map` Function

Let's start with a simple example to see how the `map` function works.

# Define a function to square a number
def square(x):
    return x * x

# Define a list of numbers
numbers = [1, 2, 3, 4, 5]

# Apply the square function to each item in the list using map
squared_numbers = map(square, numbers)

# Convert the result to a list and print it
squared_numbers_list = list(squared_numbers)
print(squared_numbers_list)
[1, 4, 9, 16, 25]
In this example, the `square` function is applied to each item in the `numbers` list using `map`. The result is a new iterable of squared numbers.

### Using `map` with Lambda Functions

You can simplify the process even more by using lambda functions with `map`. Lambda functions are anonymous functions defined with the `lambda` keyword.

# Define a list of numbers
numbers = [1, 2, 3, 4, 5]

# Use map with a lambda function to square each number
squared_numbers = map(lambda x: x * x, numbers)

# Convert the result to a list and print it
squared_numbers_list = list(squared_numbers)
print(squared_numbers_list)
[1, 4, 9, 16, 25]
In this snippet, we achieve the same result as before but without defining a separate function.

### Converting Data Types Using `map`

The `map` function is also useful for converting the data types of iterable elements.

# Define a list of strings representing numbers
str_numbers = ['1', '2', '3', '4', '5']

# Use map to convert each string to an integer
int_numbers = map(int, str_numbers)

# Convert the result to a list and print it
int_numbers_list = list(int_numbers)
print(int_numbers_list)
[1, 2, 3, 4, 5]
Here, we convert a list of string numbers into a list of integers using the built-in `int` function with `map`.

### Using Multiple Iterables with `map`

You can use `map` with multiple iterables. The provided function should accept as many arguments as there are iterables.

# Define two lists of numbers
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Define a function to add two numbers
def add(x, y):
    return x + y

# Use map to add elements from both lists
added_numbers = map(add, list1, list2)

# Convert the result to a list and print it
added_numbers_list = list(added_numbers)
print(added_numbers_list)
[5, 7, 9]
In this code, we add corresponding elements from two lists using the `add` function with `map`.

### Using `map` with Complex Data Structures

You can also use `map` to transform complex data structures, such as lists of dictionaries.

# Define a list of dictionaries
students = [
    {'name': 'Alice', 'score': 85},
    {'name': 'Bob', 'score': 90},
    {'name': 'Charlie', 'score': 78}
]

# Define a function to extract student names
def get_name(student):
    return student['name']

# Use map to extract names from the list of dictionaries
names = map(get_name, students)

# Convert the result to a list and print it
names_list = list(names)
print(names_list)
['Alice', 'Bob', 'Charlie']
In this example, the `get_name` function extracts the 'name' key from each dictionary in the `students` list, and `map` applies this function to all items.

### Combining `map` with Other Functions

You can combine `map` with other functions like [filter](/tutorials/filter) and [reduce](/tutorials/reduce) to create powerful data processing pipelines.

from functools import reduce

# Define a list of numbers
numbers = [1, 2, 3, 4, 5]

# Use map to square each number
squared_numbers = map(lambda x: x * x, numbers)

# Use filter to select only even squared numbers
even_squared_numbers = filter(lambda x: x % 2 == 0, squared_numbers)

# Use reduce to sum the even squared numbers
sum_even_squared_numbers = reduce(lambda x, y: x + y, even_squared_numbers)

print(sum_even_squared_numbers)
20
In this code snippet, `map` squares the numbers, `filter` selects only the even squared numbers, and `reduce` sums these numbers.

### Conclusion

The `map` function allows you to apply transformations, e.g. performing simple type conversions, applying complex functions, or processing mixed data types, to all elements in an iterable efficiently.