×

Syntax of Map function in Python

In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We will understand the map function with the aid of codes and examples.

Understanding the map function

A map() function is a function that yields a map entity (which is an iterator) of the outcomes after applying the given function to each item of a given iterable  (list,  tuple, set, etc.)

Syntax:

map (func, iter)

Description of Parameters:

func: It refers to a function to which a map is passed each element of a given iterable.

Iter is an iterable (list, tuple, etc.) that is to be mapped.

Here, more than one iterable is allowed to pass to the function.

Return value:

The map function returns a list of the outcomes the given function is applied to each item of a given iterable (list, tuple, etc.).

The value returned from map() function (map object) can be further sent to functions such as list() to create a list, set() to create a set.

Illustration 1:

Let us start with the basic program to get an overview of the map in Python.

Here, the numbers are getting doubled up using the map function.

# Python program to show the working of a map in Python


# Returns double of m
def add(m):
	return m + m


# We double all numbers using map()
nos = (16, 26, 43, 64)
outcome = map(add, nos)
print(list(outcome))

Output:

Syntax Of Map Function In Python

Illustration 2:

Here is the other example to understand the map even better.

# Python program to show the working of a map in Python


# Double each number by taking map and lambda functions into account


nos = (1, 2, 3, 4)
outcome = map(lambda x: x + x, nos)
print(list(outcome))

Output:

Syntax Of Map Function In Python

Illustration 3:

Here is the program to understand the map's working by using lambda along with the map function.

# Python program to understand the map in Python
# Adding two lists using map and lambda functions


nos1 = [11, 12, 13, 10, 18]
nos2 = [14, 15, 16, 17, 19]


outcome = map (lambda x, y: x + y, nos1, nos2)
print(list(outcome))

Output:

Syntax Of Map Function In Python

Illustration 4:

We will now understand the working of the Map function with the list iterator.

# Online Python-3 Compiler (Interpreter)
# Python program to understand the map in Python


# List of strings
lst = ['sun', 'mon', 'tues', 'wed', 'thurs','fri', 'sat',]


# map() function can listify the list of strings one by one
tst = list(map(list, lst))
print(tst)

Output:

Syntax Of Map Function In Python

Illustration 5:


# Python program to understand the map in Python


 def func1(a):
  return len(a)


z = map (func1, ('apple', 'banana', 'cherry', 'guava', 'watermelon'))


print(z)


# converting the map into a list to enhance the readability


print(list(z))

Output:

Syntax Of Map Function In Python

Illustration 6:

We will now understand the working of the Map function through mathematical function – math.sqrt.

#Python program to understand the map in Python


import math


nos = [144, 36, 4, 49, 81, 121, 64]


z = list (map (math.sqrt, nos) )


print(z)

Output:

Syntax Of Map Function In Python

Illustration 7:

We will now understand the working of the Map function with the tuple iterator.

#python program to understand the working of map 


def illustration(s):


    return s.upper()


tuple_example = ('It', 'is', 'available', 'on', 'tutorials', 'and', 'example', 'website')


upd_tup = map(illustration, tuple_example)


print(upd_tup)


print(tuple(upd_tup))

Output:

Syntax Of Map Function In Python

Illustration 8:

We will now understand the working of the Map function with the dictionary iterator.

 #Python program to understand the working of map


subject_dict = {'a': 'History', 'b': 'anthropology', 'c': 'polity', 'd': 'hindi', 'e': 'geography', 'f': 'english'}


print ('The original dictionary: ')
print (subject_dict)


# Adding an underscore '_' at the end of each value


subject_dict = dict (map (lambda x: (x[0], x[1] + '_'), subject_dict.items() ))


print ('The modified dictionary is: ')


print(subject_dict)

Output:

Syntax Of Map Function In Python

Illustration 9:

We will now understand the working of the Map function with the set iterator.

def example_set(z):


    return z%3


set_eg = {133, 172, 86, 90, 84, 58, 97}


upd_itms = map(example_set, set_eg)


print (upd_itms)


print (set (upd_itms))

Output:

Syntax Of Map Function In Python

Summary

In this module, we learned what a map function in Python is. We learned the syntax of the map function, which consist of two parameters one is the function, and the other is iterable, which can be anything set, dictionary, tuple, list, etc.

Further, we saw the map function in depth through the aid of various example codes written in the Python compiler.


Related Topics

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

How to get current date in python?

How to get current date in python? In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate...

3 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Reading a File Line by Line in Python

Introduction In this tutorial, we will learn about reading files in python line by line. Before reading the files, let us know a little information about the files first. Files A file is...

6 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Python String replace() method

Python String replace() method The string.replace() method in Python returns a copy of the string with all occurrences of substring old replaced by new. Syntax replace(old, new[, count]) Parameter old: This parameter represents a substring you want to...

2 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

6 minutes read.

Add Dictionary to Dictionary in Python

Dictionary in python: Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each...

4 minutes read.