Updated March 28, 2023
Introduction to NumPy map
Whenever we have a list of values or an iterable and if a particular function must be applied on each value in the given list of values or the iterable, we make use of a function called map() function in numpy, which takes two parameters function and iterable where the function is the particular function that must be applied on each value in the given list of values or the iterable and iterable is the list of values on which the particular function by name function must be applied, and map() function in numpy returns a list of values on which the specified function is applied.
Syntax of NumPy map() function in Python:
numpy.map(functionname, iterable)
Where function-name is the name of the particular function that must be applied on each value in the given list of values, or the iterable and iterable is the list of values on which the particular function by name function must be applied.
Working of NumPy map() Function
- Whenever we have a list of values or an iterable, and if a particular function must be applied on each value in the given list of values or the iterable, we make use of a function called map() function in numpy.
- The map() function in NumPy takes the two parameters function-name and iterable.
- The function name is the name of the particular function that must be applied on each value in the given list of values or the iterable.
- Iterable is the list of values on which the particular function by name function must be applied.
- The map() function in numpy returns a list of values on which the specified function is applied.
Examples of NumPy map
Given below are the examples of NumPy map:
Example #1
Python program to demonstrate NumPy map function to create a list which gives a combination of first name and last name of the given user from the two lists consisting of first names and last names alone.
Code:
#a function called user is defined which takes the two values firstname and lastname s the parameters and returns the combined value of firstname and lastname
def user(firstname, lastname):
return firstname + lastname
#map function is used by specifying the user function as the function and providing the two list of values consisting of firstnames and lastnames alone on which the user function must be applied and the resulting list is stored in a variable called username
username = map(user, ('Shobha', 'Narendra', 'Ram'), ('Shivakumar', 'Modi', 'Krishna'))
#the resulting list after using the map function is displayed as the output on the screen
print(username)
Output:
In the above program, a function called user is defined, which takes the two values firstname and lastname s the parameters and returns the combined value of firstname and lastname. Then map function is used by specifying the user function as the function and providing the two list of values consisting of firstnames and lastnames alone on which the user function must be applied, and the resulting list is stored in a variable called username. Then the resulting list after using the map function is displayed as the output on the screen.
Example #2
Python program to demonstrate NumPy map function to create a list which gives a combination of the title used before the first name and last name of the given user from the two lists consisting of titles and first names, last names alone.
Code:
#a function called user is defined which takes the two values titles and firstnamelastname s the parameters and returns the combined value of firstname and lastname
def user(title, firstnamelastname):
return title + firstnamelastname
#map function is used by specifying the user function as the function and providing the two list of values consisting of titles and firstnameslastnames alone on which the user function must be applied and the resulting list is stored in a variable called username
username = map(user, ('Ms.', 'Mr.', 'Mr'), ('ShobhaShivakumar', 'NarendraModi', 'RamKrishna'))
#the resulting list after using the map function is displayed as the output on the screen
print(username)
Output:
In the above program, a function called user is defined, which takes the two values titles and firstnamelastname as the parameters and returns the combined value of titles and firstnamelastname. Then map function is used by specifying the user function as the function and providing the two list of values consisting of titles and firstnameslastnames alone on which the user function must be applied, and the resulting list is stored in a variable called username. Then the resulting list after using the map function is displayed as the output on the screen.
Example #3
Python program to demonstrate NumPy map function to create a list that gives a sum of a number when the number is added.
Code:
#a function called addition is defined which takes a single value number and as the parameter and returns the sum of adding the number with itself
def addition(number):
return number + number
#map function is used by specifying the addition function as the function and providing the list of values consisting of numbers whose values must be added woth themselves on which the addition function must be applied and the resulting list is stored in a variable called result
result = map(addition, (2,4,6))
#the resulting list after using the map function is displayed as the output on the screen
print(result)
Output:
In the above program, a function called addition is defined, which takes a single value number as the parameter and returns the sum of adding the number. Then map function is used by specifying the addition function as the function and providing the list of values consisting of numbers whose values must be added with themselves on which the addition function must be applied, and the resulting list is stored in a variable called result. Then the resulting list after using the map function is displayed as the output on the screen.
Recommended Articles
This is a guide to NumPy map. Here we discuss the introduction, working of NumPy map() function along with examples, respectively. You may also have a look at the following articles to learn more –