The map() function is a built-in Python function used to apply a function to every item in an iterable, such as a list, tuple, or set.
Instead of manually writing a loop to process each value, map() allows us to apply the same operation to all values in a clean and reusable way.
Basic Syntax
map(function, iterable)For example:
numbers = [1, 2, 3, 4, 5]
result = map(lambda x: x * 2, numbers)
print(list(result))Output:
[2, 4, 6, 8, 10]Here:
map()→ applies a function to each itemlambda x: x * 2→ operation performed on each itemnumbers→ data being processedlist()→ converts the map result into a list
Important:
map()does not modify the original list. It creates a map object containing the transformed values.
1. Basic map() Example
numbers = [1, 2, 3, 4, 5]
result = map(lambda x: x * 2, numbers)
print(list(result))Output:
[2, 4, 6, 8, 10]Each number is multiplied by 2.
1 → 2
2 → 4
3 → 6
4 → 8
5 → 102. map() with a Normal Function
map() does not require a lambda. We can pass a normal function too.
def square(number):
return number * number
numbers = [1, 2, 3, 4, 5]
result = map(square, numbers)
print(list(result))Output:
[1, 4, 9, 16, 25]This is useful when the operation is large enough to deserve its own function.
3. map() with Lambda
For a small operation, lambda is convenient.
numbers = [2, 4, 6, 8]
result = map(
lambda x: x + 5,
numbers
)
print(list(result))Output:
[7, 9, 11, 13]4. Convert Strings to Uppercase
map() can also work with strings.
names = [
"raj",
"john",
"sita",
"michael"
]
result = map(
lambda name: name.upper(),
names
)
print(list(result))Output:
['RAJ', 'JOHN', 'SITA', 'MICHAEL']5. Convert Strings to Lowercase
names = [
"RAJ",
"JOHN",
"SITA"
]
result = map(
lambda name: name.lower(),
names
)
print(list(result))Output:
['raj', 'john', 'sita']6. map() and str.title()
names = [
"rajendra kandel",
"john doe",
"sita sharma"
]
result = map(
lambda name: name.title(),
names
)
print(list(result))Output:
['Rajendra Kandel', 'John Doe', 'Sita Sharma']7. Convert Strings to Integers
Suppose numbers are stored as strings:
numbers = ["10", "20", "30", "40"]We can convert them into integers.
result = map(int, numbers)
print(list(result))Output:
[10, 20, 30, 40]This is one of the most useful real-world applications of map().
8. Convert Strings to Float
prices = [
"10.5",
"25.75",
"100.50"
]
result = map(float, prices)
print(list(result))Output:
[10.5, 25.75, 100.5]9. Calculate Squares
numbers = [1, 2, 3, 4, 5]
squares = map(
lambda x: x ** 2,
numbers
)
print(list(squares))Output:
[1, 4, 9, 16, 25]10. Calculate Cubes
numbers = [1, 2, 3, 4, 5]
cubes = map(
lambda x: x ** 3,
numbers
)
print(list(cubes))Output:
[1, 8, 27, 64, 125]11. Add a Fixed Value
numbers = [10, 20, 30, 40]
result = map(
lambda x: x + 100,
numbers
)
print(list(result))Output:
[110, 120, 130, 140]12. Calculate Percentage
Suppose students have obtained marks out of 100.
marks = [45, 65, 72, 88, 95]
percentage = map(
lambda mark: mark / 100 * 100,
marks
)
print(list(percentage))Since the total is 100, the result is the same as the marks.
A more useful example:
marks = [45, 65, 72, 88, 95]
percentage = map(
lambda mark: mark / 150 * 100,
marks
)
print(list(percentage))13. Add Tax to Prices
prices = [1000, 2000, 5000, 10000]
prices_with_tax = map(
lambda price: price * 1.13,
prices
)
print(list(prices_with_tax))This adds 13% tax.
14. Apply Discount
prices = [1000, 2000, 5000]
discounted = map(
lambda price: price * 0.9,
prices
)
print(list(discounted))This applies a 10% discount.
15. map() with Tuples
map() can process tuples too.
numbers = (10, 20, 30, 40)
result = map(
lambda x: x * 2,
numbers
)
print(list(result))Output:
[20, 40, 60, 80]16. map() with Sets
numbers = {10, 20, 30, 40}
result = map(
lambda x: x + 1,
numbers
)
print(list(result))The order should not be relied upon because sets are unordered collections.
17. map() with Multiple Lists
One of the more powerful features of map() is that it can work with multiple iterables.
numbers1 = [10, 20, 30]
numbers2 = [1, 2, 3]
result = map(
lambda a, b: a + b,
numbers1,
numbers2
)
print(list(result))Output:
[11, 22, 33]The operation happens like this:
10 + 1 = 11
20 + 2 = 22
30 + 3 = 3318. Multiply Two Lists
prices = [100, 200, 300]
quantity = [2, 3, 4]
total = map(
lambda price, qty: price * qty,
prices,
quantity
)
print(list(total))Output:
[200, 600, 1200]This is useful for calculating item totals.
19. Three Lists with map()
price = [100, 200, 300]
quantity = [2, 3, 4]
discount = [10, 20, 5]
total = map(
lambda p, q, d: p * q - (p * q * d / 100),
price,
quantity,
discount
)
print(list(total))20. map() with Dictionary Data
Suppose we have:
students = [
{"name": "Raj", "marks": 85},
{"name": "John", "marks": 72},
{"name": "Sita", "marks": 91}
]Get only the student names:
names = map(
lambda student: student["name"],
students
)
print(list(names))Output:
['Raj', 'John', 'Sita']21. Get Student Marks
marks = map(
lambda student: student["marks"],
students
)
print(list(marks))Output:
[85, 72, 91]22. Create a New Student Result
We can transform each dictionary into another dictionary.
students = [
{"name": "Raj", "marks": 85},
{"name": "John", "marks": 72},
{"name": "Sita", "marks": 91}
]
result = map(
lambda student: {
"name": student["name"],
"result": "Pass" if student["marks"] >= 40 else "Fail"
},
students
)
print(list(result))Output:
[
{'name': 'Raj', 'result': 'Pass'},
{'name': 'John', 'result': 'Pass'},
{'name': 'Sita', 'result': 'Pass'}
]23. map() Does Not Change the Original Data
numbers = [1, 2, 3, 4]
result = map(
lambda x: x * 10,
numbers
)
print("Original:", numbers)
print("New:", list(result))Output:
Original: [1, 2, 3, 4]
New: [10, 20, 30, 40]The original list remains unchanged.
24. The Result of map()
If we directly print the result:
numbers = [1, 2, 3]
result = map(
lambda x: x * 2,
numbers
)
print(result)You will see something similar to:
<map object at 0x...>That's because map() returns a map object, not a normal list.
To see the values:
print(list(result))25. map() Is Lazy
A map object produces values when we consume it.
For example:
numbers = [1, 2, 3, 4]
result = map(
lambda x: x * 2,
numbers
)
for value in result:
print(value)Output:
2
4
6
8This behavior can be useful when processing large amounts of data because the transformed values don't all have to be stored in a new list immediately.
26. map() with a Normal Function : Practical Example
def calculate_salary(salary):
return salary + 5000
salaries = [
25000,
30000,
40000,
50000
]
updated_salary = map(
calculate_salary,
salaries
)
print(list(updated_salary))Output:
[30000, 35000, 45000, 55000]27. Practical Example : Course Prices
courses = [
15000,
18000,
22000,
25000
]
discounted_prices = map(
lambda price: price * 0.9,
courses
)
print(list(discounted_prices))28. Practical Example : SkillMantra Courses
courses = [
{
"name": "Python",
"price": 15000
},
{
"name": "Django",
"price": 18000
},
{
"name": "MERN",
"price": 22000
}
]
updated_courses = map(
lambda course: {
"name": course["name"],
"price": course["price"] * 0.9
},
courses
)
for course in updated_courses:
print(course)Copy Code
# ========================================== # Python map() Function # ==========================================
# ------------------------------------------ # 1. Basic map() # ------------------------------------------
numbers = [1, 2, 3, 4, 5]
result = map(
lambda x: x * 2,
numbers
)
print(list(result))
# ------------------------------------------ # 2. map() with normal function # ------------------------------------------
def square(number):
return number * number
numbers = [1, 2, 3, 4, 5]
result = map(
square,
numbers
)
print(list(result))
# ------------------------------------------ # 3. Convert strings to uppercase # ------------------------------------------
names = [
"raj",
"john",
"sita",
"michael"
]
result = map(
lambda name: name.upper(),
names
)
print(list(result))
# ------------------------------------------ # 4. Convert strings to lowercase # ------------------------------------------
names = [
"RAJ",
"JOHN",
"SITA"
]
result = map(
lambda name: name.lower(),
names
)
print(list(result))
# ------------------------------------------ # 5. Convert strings to integers # ------------------------------------------
numbers = [
"10",
"20",
"30",
"40"
]
result = map(
int,
numbers
)
print(list(result))
# ------------------------------------------ # 6. Convert strings to float # ------------------------------------------
prices = [
"10.5",
"25.75",
"100.50"
]
result = map(
float,
prices
)
print(list(result))
# ------------------------------------------ # 7. Calculate squares # ------------------------------------------
numbers = [1, 2, 3, 4, 5]
result = map(
lambda x: x ** 2,
numbers
)
print(list(result))
# ------------------------------------------ # 8. Calculate cubes # ------------------------------------------
numbers = [1, 2, 3, 4, 5]
result = map(
lambda x: x ** 3,
numbers
)
print(list(result))
# ------------------------------------------ # 9. Add a fixed value # ------------------------------------------
numbers = [10, 20, 30, 40]
result = map(
lambda x: x + 100,
numbers
)
print(list(result))
# ------------------------------------------ # 10. Apply discount # ------------------------------------------
prices = [1000, 2000, 5000]
result = map(
lambda price: price * 0.9,
prices
)
print(list(result))
# ------------------------------------------ # 11. Apply tax # ------------------------------------------
prices = [1000, 2000, 5000]
result = map(
lambda price: price * 1.13,
prices
)
print(list(result))
# ------------------------------------------ # 12. map() with tuple # ------------------------------------------
numbers = (10, 20, 30, 40)
result = map(
lambda x: x * 2,
numbers
)
print(list(result))
# ------------------------------------------ # 13. map() with multiple lists # ------------------------------------------
numbers1 = [10, 20, 30]
numbers2 = [1, 2, 3]
result = map(
lambda a, b: a + b,
numbers1,
numbers2
)
print(list(result))
# ------------------------------------------ # 14. Multiply two lists # ------------------------------------------
prices = [100, 200, 300]
quantity = [2, 3, 4]
result = map(
lambda price, qty: price * qty,
prices,
quantity
)
print(list(result))
# ------------------------------------------ # 15. Three lists # ------------------------------------------
price = [100, 200, 300]
quantity = [2, 3, 4]
discount = [10, 20, 5]
result = map(
lambda p, q, d:
p * q - (p * q * d / 100),
price,
quantity,
discount
)
print(list(result))
# ------------------------------------------ # 16. Dictionary data # ------------------------------------------
students = [
{"name": "Raj", "marks": 85},
{"name": "John", "marks": 72},
{"name": "Sita", "marks": 91}
]
names = map(
lambda student: student["name"],
students
)
print(list(names))
# ------------------------------------------ # 17. Get marks # ------------------------------------------
marks = map(
lambda student: student["marks"],
students
)
print(list(marks))
# ------------------------------------------ # 18. Create result data # ------------------------------------------
result = map(
lambda student: {
"name": student["name"],
"result":
"Pass"
if student["marks"] >= 40
else "Fail"
},
students
)
print(list(result))
# ------------------------------------------ # 19. map() with for loop # ------------------------------------------
numbers = [1, 2, 3, 4]
result = map(
lambda x: x * 10,
numbers
)
for value in result:
print(value)
# ------------------------------------------ # 20. Course prices # ------------------------------------------
courses = [
{
"name": "Python",
"price": 15000
},
{
"name": "Django",
"price": 18000
},
{
"name": "MERN",
"price": 22000
}
]
updated_courses = map(
lambda course: {
"name": course["name"],
"price": course["price"] * 0.9
},
courses
)
for course in updated_courses:
print(course)