KnightOfNi
KnightOfNi

Reputation: 850

Why use the map() function?

I was working with Python, and I noticed that the map() function doesn't really seem to do much. For instance, if I write the program:

mylist = [1, 2, 3, 4, 5]
function = map(print, l)
print(function)

It provides no advantages over:

mylist = [1, 2, 3, 4, 5]
for item in mylist:
    print(item)

In fact, the second option creates fewer variables and seems generally cleaner overall to me. I would assume that map() provides advantages that cannot be seen in an example as simplistic as this one, but what exactly are they?

EDIT: It seems that some people have been answering a different question than the one I intended to ask. The developers who made Python obviously put some work into creating the map() function, and they even decided NOT to take it out of 3.0, and instead continue working on it. What essential function did they decide it served?

Upvotes: 11

Views: 6627

Answers (4)

Mauro Baraldi
Mauro Baraldi

Reputation: 6550

According to map's documentation: "Return an iterator that applies function to every item of iterable, yielding the results."

No packed code:

def square(x):
    return x**2

def calculist(lista):
    for n in lista:
        yield square(n)

numbers = [i for i in calculist(range(10))]

One line code:

numbers = map(lambda x: x**2, range(10))

As filter, beyond others, map is a built-in function with focus on functional programing. It may be useful for hacks and tricks, because it shorten the code, but can be a bad idea for large projects in use.

You can get more fun on Functional Programming HOWTO from official doc.

Upvotes: 1

Martin Konecny
Martin Konecny

Reputation: 59571

It can be much less verbose. Imagine you want to add + 1 to each element in a list of ints.

old_list = [1, 2, 3, 4]
new_list = []
for i in old_list:
    new_list.append(i+1)

vs.

old_list = [1, 2, 3, 4]
new_list = map(lambda x: x+1, old_list)

You say the map syntax appears much less clear to you. However, mapping is used so often in applications, that sooner you will appreciate how small and concise it is.

It really helps if you think of map as a function that takes in an input list of size N, and outputs a new list of the exact same length with a transformation done on each element. If you keep this in mind, everytime you see a map function, you know exactly what is about to happen.

You will notice that Python has some other functional programming functions such as reduce, filter, zip etc. map is part of this class of functions where although each implements a very simple function, when you combine these into a single statement you can get very powerful results. With a for loop you cannot achieve the same expressiveness so succinctly.

Upvotes: 2

Cory Kramer
Cory Kramer

Reputation: 117856

One very common use is converting types of an entire list

>>> myList = ['1', '2', '3', '4']  # currently strings
>>> myList
['1', '2', '3', '4']

>>> map(int, myList)   # now ints
[1, 2, 3, 4]

>>> map(float, myList)  # now floats
[1.0, 2.0, 3.0, 4.0]

Another very common use is passing a list through some function

>>> def addToFive(x):
>>>     return x + 5

>>> l = [1,2,3,4,5]

>>> map(addToFive, l)
[6, 7, 8, 9, 10]

Upvotes: 1

user2357112
user2357112

Reputation: 280182

It used to be more useful back before list comprehensions. You've probably seen code like

[int(x[2]) for x in l]

Back before list comprehensions, you'd write that as

map(lambda x: int(x[2]), l)

(This was also back before map returned an iterator.)

These days, list comprehensions and generator expressions handle most of what map used to do. map is still cleaner sometimes, particularly when you don't need a lambda. For example, some people prefer

map(str, l)

over

(str(x) for x in l)

Upvotes: 9

Related Questions