Thursday, 6 August 2026

πŸš€ Day 96/150 – map() Function in Python

 



πŸš€ Day 96/150 – map() Function in Python

The map() function is a built-in Python function used to apply a function to every item in an iterable, such as a list or tuple. It helps you write cleaner and more concise code by avoiding explicit loops.

Syntax:

map(function, iterable)

In this post, we'll explore four common examples of using the map() function in Python.


Method 1 – Using map() with a Normal Function

Apply a normal function to every element in a list.

def square(num): return num ** 2 numbers = [1, 2, 3, 4, 5] result = list(map(square, numbers)) print(result)








Output

[1, 4, 9, 16, 25]

Explanation

  • square() returns the square of a number.
  • map() applies the square() function to every element in numbers.
  • list() converts the map object into a list.

Method 2 – Using map() with a Lambda Function

Use a lambda function for shorter code.

numbers = [2, 4, 6, 8] result = list(map(lambda x: x * 2, numbers)) print(result)





Output

[4, 8, 12, 16]

Explanation

  • lambda x: x * 2 doubles each element.
  • map() applies the lambda function to every item in the list.
  • The result is converted into a list.

Method 3 – Using map() with Multiple Iterables

map() can process multiple iterables at the same time.

list1 = [1, 2, 3] list2 = [4, 5, 6] result = list(map(lambda x, y: x + y, list1, list2)) print(result)






Output

[5, 7, 9]

Explanation

  • map() takes one element from each list at the same position.
  • The lambda function adds the corresponding elements.
  • The result is returned as a new list.

Method 4 – Taking User Input

Use map() to convert multiple user inputs into integers.

numbers = list(map(int, input("Enter numbers separated by spaces: ").split())) print(numbers)




Sample Input

10 20 30 40

Output

[10, 20, 30, 40]

Explanation

  • input() reads the values as a string.
  • split() separates the string into a list of strings.
  • map(int, ...) converts each string into an integer.
  • list() stores the converted values in a list.

Comparison of Methods

MethodBest For
Normal FunctionReusing existing functions
Lambda FunctionShort and simple operations
Multiple IterablesProcessing two or more lists together
User InputConverting input values to the desired data type

πŸ”₯ Key Takeaways

  • map() applies a function to every element in an iterable.
  • It returns a map object, which is often converted to a list using list().
  • map() works with both normal functions and lambda functions.
  • It can process multiple iterables simultaneously.
  • map() makes code cleaner and often replaces explicit for loops for simple transformations.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (330) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (321) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (302) Cybersecurity (34) data (10) Data Analysis (44) Data Analytics (31) data management (16) Data Science (414) Data Strucures (18) Deep Learning (212) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (12) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (373) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1354) Python Coding Challenge (1208) Python Mathematics (8) Python Mistakes (51) Python Quiz (591) Python Tips (99) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (54) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)