×

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

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Pltpcolor in 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...

3 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Sklearn in Python

Scikit-learn or sklearn is a machine learning library used in Python that provides many unsupervised and supervised learning tools and algorithms. David Cournapeau first created it as a 2007 Google...

7 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Python String encode() method

Python String encode() method The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding Syntax String.encode([encoding[,errors]]) Parameter Encoding: This parameter represents a String specifying...

2 minutes read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Python AIOHTTP

Python 3.5 introduced some new syntax that makes it simpler for developers to make asynchronous programmes and packages. Aiohttp, an HTTP client/server for asyncio, is one such package. In essence,...

3 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

Difference Between Python and Scala

What is Python? Python is a high-level general-purpose interpreted programing language. It is used for multi general-purpose work such as language construct as well as its object-oriented approach aims to help...

4 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Rank Based Percentile GUI Calculator using Tkinter in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

3 minutes read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.