Lesson 1
Exploring the map Function
Lesson Introduction

Welcome! Today we'll explore the map function in Python. The map function is a powerful tool in functional programming that applies a function to each item in an iterable, such as a list or tuple. By the end of this lesson, you'll know how to use the map function effectively to write more concise and readable code.

We’ll focus on:

  • Understanding the basics of the map function.
  • Using lambda functions and predefined functions with map.
  • Working with multiple iterables using map.

Ready? Let's dive in!

Basics of the map Function

What is the map function? map applies a given function to each item of an iterable (like a list), returning a new iterable with the results. The basic syntax is:

Python
1map(function, iterable)

Here is a simple example:

Python
1# Define a function to double the input value 2def double(x): 3 return x * 2 4 5if __name__ == "__main__": 6 numbers = [1, 2, 3, 4, 5] 7 8 # Apply the double function to each element in the numbers list 9 doubled_numbers = map(double, numbers) 10 print(list(doubled_numbers)) # [2, 4, 6, 8, 10]

In the above code, the function double is applied to each element in the numbers list. The map function returns a special iterable object called map. We convert it back to a list to see the result.

Applying Lambda Functions with map

Lambda functions are small anonymous functions defined using the lambda keyword. They are often used for simple operations. Let's recall their syntax:

Python
1lambda arguments: expression

When used with map, lambda functions can simplify our code:

Python
1if __name__ == "__main__": 2 numbers = [1, 2, 3, 4, 5] 3 4 # Use map with a lambda function to square each number 5 squared = map(lambda x: x ** 2, numbers) 6 print(list(squared)) # [1, 4, 9, 16, 25]

In this case, the lambda function lambda x: x ** 2 squares each number in the numbers list.

Multiple Iterables with map

map can accept multiple iterables. The function passed to map should accept the same number of arguments as there are iterables. Here's an example:

Python
1if __name__ == "__main__": 2 numbers1 = [1, 2, 3] 3 numbers2 = [4, 5, 6] 4 5 # Use map with a lambda function that adds corresponding elements from two lists 6 summed = map(lambda x, y: x + y, numbers1, numbers2) 7 print(list(summed)) # [5, 7, 9]

In this code, the lambda function lambda x, y: x + y takes two arguments and adds them together. The map function applies this lambda to corresponding elements of numbers1 and numbers2.

Lesson Summary

Great job! In this lesson, we explored the map function in Python. We covered:

  • The basic syntax and use of the map function.
  • Using lambda functions with map for concise operations.
  • Applying map to multiple iterables at once.

Now it’s time to put this knowledge into practice. The upcoming practice tasks will solidify your understanding and help you apply what you've learned.

Ready to map your new knowledge into practice? Let’s get started!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.