The map()
function:
The map()
function in Python is used to apply a function to every item in an iterable—like a list or a tuple—and returns a new map object with the results.
Syntax:
map(function, iterable)
function
: This is the function you want to apply to each item.iterable
: This is the list, tuple, or any other iterable whose items you want to process.
Basic Example:
Imagine we want to square every number in a list.
Without map()
:
numbers = [1, 2, 3]
squared = []
for num in numbers:
square = num ** 2
squared.append(square)
print(squared)
[1, 4, 9]
Without map()
:
[1, 4, 9]
The map
a = [1, 2, 3]
b = [4, 5, 6]
summs = []
for count in range(len(a)):
summs.append(a[count] + b[count])
print(summs)
[5, 7, 9]
[5, 7, 9]
[5, 7, 9]