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:
- A function (which contains instructions for what to do)
- 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
squarefunction - It collects all the results in order
You can think of it like an assembly line:
- Number 1 goes in → square function makes it 1
- Number 2 goes in → square function makes it 4
- 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
EasyCreate 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)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter