Menu
Coddy logo textTech

The Map Function

Part of the Logic & Flow section of Coddy's Python journey — lesson 72 of 78.

The map() function takes two main things:

  1. A function (which contains instructions for what to do)
  2. A sequence of items (like a list, or any collection of elements)

It then applies that function to each item in your sequence, one by one, and gives you back all the results.

For example:

def square(n):
    return n * n

numbers = [1, 2, 3, 4, 5]  # This is just a list of numbers
squared_numbers = map(square, numbers)
print(list(squared_numbers))
# Output: [1, 4, 9, 16, 25]

What happened here:

  • We have a list of numbers: [1, 2, 3, 4, 5]
  • The map() function takes each number, one at a time
  • It puts each number through our square function
  • It collects all the results in order

You can think of it like an assembly line:

  1. Number 1 goes in → square function makes it 1
  2. Number 2 goes in → square function makes it 4
  3. Number 3 goes in → square function makes it 9 And so on...

You can also use a lambda function instead of defining a separate function:

numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda n: n * n, numbers)
print(list(squared_numbers))
# Output: [1, 4, 9, 16, 25]

The map() function works with any collection of items - not just numbers. It could be strings, or any other type of data you want to process in the same way.

challenge icon

Challenge

Easy

Create a function named convert_to_uppercase that takes a list of strings strings as an argument. The function should use the map() function along with a lambda function to convert each string in the list to uppercase. The function should return a list containing the uppercase strings.

Cheat sheet

The map() function applies a function to each item in a sequence and returns the results:

def square(n):
    return n * n

numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
print(list(squared_numbers))
# Output: [1, 4, 9, 16, 25]

You can also use lambda functions with map():

numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda n: n * n, numbers)
print(list(squared_numbers))
# Output: [1, 4, 9, 16, 25]

The map() function works with any type of data, not just numbers.

Try it yourself

def convert_to_uppercase(strings):
    # Use map() with a lambda function to convert strings to uppercase
    uppercase_strings = 
    
    # Return the list of uppercase strings
    return list(uppercase_strings)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow