In this blog, we will be seeing what the Python map() function is. We will also, get to learn about the filter() and the reduce() functions by the help of quite a number of examples. Examples ranging from list comprehensions, to string mapping and other such items too.

The Python map() function applies any function on each item of the iterable and returns the map object. The Python map object is an iterator, used to iterate over all the items. We can also convert map object to sequence objects such as tuples, lists, dictionaries, etc.

Python map function

Python map()

The Python map() function applies any function on each item of the iterable and returns the map object, i.e., the list of the results.

The syntax of the map() function is:

map(function, iterable, ...)

The map() parameters

  • function – the map() function is generally used to pass all items of the iterable to the function.
  • iterable – it is the iterable which must be mapped

One can though, pass more than one iterable item to map() .

The value to return from the map() function

The Python map() function applies any function on each item of the iterable and returns the map object, i.e., the list of the results.

The values from the map() are generally returned to list(), set() and other such functions as may be deemed required by the user.


How the map() function works

def Square(a):
    return a*a


num = (4, 3, 2, 5)
result = map(lambda x: x*x, num)
print(result)

# converting map object to set
square = set(result)
print(square)

Output

<map object at 0x7f722da129e8>
{16, 9, 4, 25}

This program above, square all the items of the tuples. This is generally by using lambda() functions as the map() function requires a function to pass to it. Thus, the no-name lambda() function is pass to it. It is quite short and easy to comprehend and use, thus it is use.

To know about list comprehension read here …..

>>> numbers = [-4, 3, 0, -1, 5]

>>> abs_values = list(map(abs, numbers))
>>> abs_values
[4, 3, 0, 1, 5]

>>> list(map(float, numbers))
[-4.0, 3.0, 0.0, -1.0, 5.0]

>>> words = ["Good", "to", "see", "you"]

>>> list(map(len, words))
[4, 2, 3, 3]

Using lambda() function with map() function

num = (4, 3, 2, 5)
result = map(lambda x: x*x, num)
print(result)

# converting map object to set
square = set(result)
print(square)

Output

<map 0x7fafc21ccb00>
{16, 9, 4, 25}

Using the Methods of str()

A general approach to manipulating strings is by using the various methods of str class. When working with string iterables to transform each string, one needs to use map() along with the many string methods.

>>> letters = ["shubham", "working", "with", "strings"]
>>> list(map(str.capitalize, letters))
['Shubham', 'Working', 'With', 'Strings']

>>> list(map(str.upper, letters))
['SHUBHAM', 'WORKING', 'WITH', 'STRINGS']

>>> list(map(str.lower, letters))
["shubham", "working", "with", "strings"]

>>> first = ["shubham..", "...working", "with....", "..strings.."]

>>> list(map(lambda s: s.strip("."), first))
["shubham", "working", "with", "strings"]

Passing Multiple Iterators to map()

# map() with multiple iterable arguments
list_num = [4, 5, 6, 7]
tuple_num = (5, 6, 7, 8)

iterator = map(lambda x, y: x * y, list_num, tuple_num)
print(list(iterator))

Output

[20, 30, 42, 56]
first = [12, 51, 36]
second =[52, 11, 67]

results = map(lambda a, b: a+b, first, second)
print(list(results))

Output

[64, 62, 103]
Python map function

The filter() Function

The filter() function is thus, similar to the map() function. Also, it takes an object and an iterable to create a new list.

Thus, the filter() functions creates a new list based on satisfying certain conditions.

filter(function, iterable(s))
name = ["Shubham", "Soumya", "Manish", "Ram", "Shyam"]
object = filter(lambda s: s[0] == "S", name)

print(list(object))

Output :

['Shubham', 'Soumya', 'Shyam]

reduce() Function

The reduce() function, thus, works quite opposite to the map() and filter() fuctions. It returns a single value, instead of a list.

The reduce() function isn’t a built-in function, and is generally found in the functools module.

The syntax is:

reduce(function, sequence[, initial])

SUMMING UP

In this article, we have discussed what the map() function is. We also saw some examples of the reduce() and filter() functions too. We have thus, seen these concepts with the help of ample number of examples. Like it’s uses with list comprehensions and the lambda function and many others also. Thus, I have tried my best to make this topic clear to you. But, in case, you still have some doubts lingering. Then, please do write to me in the comments section and I am as always, ever-ready to help you. And, also solve your queries and problems.

Categorized in: