Python map return map object

Python map() function with EXAMPLES

Python map() applies a function on all the items of an iterator given as input. An iterator, for example, can be a list, a tuple, a set, a dictionary, a string, and it returns an iterable map object. Python map() is a built-in function.

Syntax

map(function, iterator1,iterator2 . iteratorN)

Parameters

Here are two important

  • function: A mandatory function to be given to map, that will be applied to all the items available in the iterator.
  • iterator: An iterable compulsory object. It can be a list, a tuple, etc. You can pass multiple iterator objects to map() function.

Return Value

The map() function is going to apply the given function on all the items inside the iterator and return an iterable map object i.e a tuple, a list, etc.

How map() function works?

The map() function takes two inputs as a function and an iterable object. The function that is given to map() is a normal function, and it will iterate over all the values present in the iterable object given.

Читайте также:  Методы предварительной обработки данных python

For example, consider you have a list of numbers, and you want to find the square of each of the numbers.

The get the output, we need the function that will return the square of the given number. The function will be as follows:

The list of items that we want to find the square is as follows:

Now let us use map() python built-in function to get the square of each of the items in my_list.

The final code is as follows:

def square(n): return n*n my_list = [2,3,4,5,6,7,8,9] updated_list = map(square, my_list) print(updated_list) print(list(updated_list))

The output of the map() function, as seen in the output, is a map object displayed on the screen as .

You will have to iterate the output from the map using a for-loop or using list() method to get the final output. I have used list() in the code that displays the values inside the list given.

So using map() function, we are able to get the square of each number.The list given to map was [2,3,4,5,6,7,8,9] and using the function square() the output from map() we got is [4, 9, 16, 25, 36, 49, 64, 81] .

The function map() applies the function square() on all the items on the list. For example, my_list variable and updates the list with the square of each number. The out is stored in the updated_list variable.

Using map() with Python built-in functions

Python map() function is a built-in function and can also be used with other built-in functions available in Python. In the example, we are going to make use of Python round() built-in function that rounds the values given.

The list that i have is my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907] .

I need the rounded values for each item present in the list. We will make use of round() as the function to map().

my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907] updated_list = map(round, my_list) print(updated_list) print(list(updated_list))

The round() function is applied to all the items in the list, and it returns back a list with all values rounded as shown in the output.

Using map() with a string as an iterator

We can also make use of map() on a string. In Python, a string acts like an array so we can easily use it inside the map().

In the example, we have a function myMapFunc() that takes care of converting the given string to uppercase. The function myMapFunc () is given to map() function.The map function will take care of converting the string given to uppercase by passing the string to myMapFunc().

def myMapFunc(s): return s.upper() my_str = "welcome to guru99 tutorials!" updated_list = map(myMapFunc, my_str) print(updated_list) for i in updated_list: print(i, end="")
 WELCOME TO GURU99 TUTORIALS!

Using map() with listof Numbers

To work with the list in map() will take a list of numbers and multiply each number in the list by 10.

The list that we are going to use is : [2,3,4,5,6,7,8,9]. The function myMapFunc () takes care of multiply the given number with 10. The function is given to map along with the list.

def myMapFunc(n): return n*10 my_list = [2,3,4,5,6,7,8,9] updated_list = map(myMapFunc, my_list) print(updated_list) print(list(updated_list))

The output we see is that each number in the list is

Using map() with Tuple

A tuple is an object in Python that has items separated by commas and enclosed in round brackets. In the example, we will take a tuple with string values. The function that we will use will convert the values given to uppercase.

def myMapFunc(n): return n.upper() my_tuple = ('php','java','python','c++','c') updated_list = map(myMapFunc, my_tuple) print(updated_list) print(list(updated_list))

The output we get is a tuple back with all the values in it are converted to uppercase.

Using map() with Dictionary

A dictionary in Python is created using curly brackets(<>). Since the dictionary is an iterator, you can make use of it inside map() function. Let us now use a dictionary as an iterator inside map() function.

Following example shows the working of dictionary iterator inside map()

def myMapFunc(n): return n*10 my_dict = finalitems = map(myMapFunc, my_dict) print(finalitems) print(list(finalitems))

Using map() with Set

Set in Python is an unordered collection of items in curly brackets(()). Since set() is also an iterator, you can make use of it inside map() function.

Here is a working example of using set as an iterator inside map()

def myMapFunc(n): return n*10 my_set = finalitems = map(myMapFunc, my_set) print(finalitems) print(list(finalitems))

Using map() with Lambda function

In Python, lambda expressions are utilized to construct anonymous functions. You will have to use the lambda keyword just as you use def to define normal functions.

So in the example, we are going to use the lambda function inside the map(). The lambda function will multiply each value in the list with 10.

my_list = [2,3,4,5,6,7,8,9] updated_list = map(lambda x: x * 10, my_list) print(updated_list) print(list(updated_list))

Using Multiple Iterators inside map() function

Example 1: Passing two list iterators to map()

You can send more than one iterator i.e. a list, a tuple, etc. all at the same time to the map() function.

For example, if you want to add two lists. The same can be done using the map() function. We are going to make use of two lists my_list1 and my_list2.

In the example below, the first item in the my_list1 is added to the first item of my_list2. The function myMapFunc() takes in items of my_list1 and my_list2 and returns the sum of both.

Here is the working example of adding two given lists using map() function.

def myMapFunc(list1, list2): return list1+list2 my_list1 = [2,3,4,5,6,7,8,9] my_list2 = [4,8,12,16,20,24,28] updated_list = map(myMapFunc, my_list1,my_list2) print(updated_list) print(list(updated_list))

Example 2: Passing one Tuple and a list iterator to map()

We are going to make use of a list and a tuple iterator in map() function. The function is given to map – myMapFunc() will get the items from the list and Tuple. The items will be joined with an underscore(_). The working example is as shown below:

def myMapFunc(list1, tuple1): return list1+"_"+tuple1 my_list = ['a','b', 'b', 'd', 'e'] my_tuple = ('PHP','Java','Python','C++','C') updated_list = map(myMapFunc, my_list,my_tuple) print(updated_list) print(list(updated_list))
 ['a_PHP', 'b_Java', 'b_Python', 'd_C++', 'e_C']

Summary

  • Python map() is a built-in function that applies a function on all the items of an iterator given as input. An iterator, for example, can be a list, a tuple, a string, etc. and it returns an iterable map object.
  • The map() function is going to apply the given function on all the items inside the iterator and return an iterable map object i.e a tuple, a list, etc.
  • Python map() function is a built-in function and can also be used with other built-in functions available in Python.
  • A tuple is an object in Python that has items separated by commas and enclosed in round brackets. In the example will take a tuple with string values. The function that we will use will convert the values given to uppercase.
  • A dictionary in Python is created using curly brackets(<>). Since the dictionary is an iterator, you can make use of it inside map() function.
  • Set in Python is an unordered collection of items in curly brackets(()). Since set() is also an iterator, you can make use of it inside map() function.
  • In Python, lambda expressions (or lambda forms) are utilized to construct anonymous functions. So the lambda keyword has to used when you want to use lambda inside the map().
  • You can send more than one iterator i.e. a list, a tuple to the map() function.

Источник

Mapping Python Lists, Dicts, and Tuples

Mapping in Python means applying an operation for each element of an iterable, such as a list.

For example, let’s square a list of numbers using the map() function:

numbers = [1, 2, 3, 4, 5] squared_nums = map(lambda x: x ** 2, numbers) print(list(squared_nums))

Mapping in Python

Mapping means transforming a group of values into another group of values.

In Python, you can use the built-in map() function for mapping. The map() function returns a map object. This map object is the result of applying an operation on an iterable, such as a list. You can easily convert this map object back to a list for example by calling the list() function on it.

The syntax of using the map() function:

Where the operation is a function, or a lambda function, whichever you prefer. The iterable is the group of items for which you apply the operation .

Mapping Python Lists

The mapping works for any iterable in Python. In other words, you can use it on a list.

For example, let’s square a list of numbers. This approach uses a lambda function as the mapping function. If you are unfamiliar with lambdas, feel free to check this guide, or see the next example without lambdas.

numbers = [1, 2, 3, 4, 5] squared_nums = map(lambda x: x ** 2, numbers) print(list(squared_nums))

Here is the same example. This time we are not using a lambda function, but a regular function instead:

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

This is a result of applying the function square() for each element of the list of numbers. Notice how you don’t need to give the square a parameter in the map function. This is possible because the map function knows what you’re trying to do. It automatically passes each element as an argument to the function one by one.

Mapping Python Dictionaries

You can also map dictionaries in Python using the built-in map() function.

For example, let’s map data such that the values of the key-value pairs become capitalized strings:

data = < "name": "jack", "address": "imaginary street", "education": "mathematican" >def capitalize(word): return word.capitalize() data_map = map(lambda pair: (pair[0], capitalize(pair[1])), data.iteritems()) data = dict(data_map) print(dict(data))

There are quite a few lines of code, so let’s clarify how it works:

  • There is data , which is a dictionary of key-value pairs. The values are not capitalized and we want to change that.
  • The capitalize() function takes a string and returns a capitalized version of it.
  • The data_map is a map object. It’s created by applying the capitalize() function for each value of each key-value pair in the data .
  • To convert the data_map back to a dictionary, we use the built-in dict() function.

Mapping Python Tuples

You can also map tuples in Python. This works very similarly to mapping a list.

For example, let’s create a tuple by capitalizing the names of another tuple:

names = ("jamie", "jane", "jack") def capitalize(word): return word.capitalize() capitalized_names = map(capitalize, names) print(tuple(capitalized_names))

Conclusion

In Python, you can use mapping to transform a group of values into another. To do this use the built-in map() function. This function works by applying a function for each element of the group of values.

For example, you can create a list of squared numbers from a list of numbers using map() :

numbers = [1, 2, 3, 4, 5] squared_nums = map(lambda x: x ** 2, numbers) print(list(squared_nums))

Thanks for reading. I hope you enjoy it.

Источник

Оцените статью