11

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?

7
  • map is C code so it's faster Commented Jun 8, 2014 at 21:09
  • 1
    Note: function = map(print, l); print(function) is not how you would use map to print the elements of a list, even if using map for that were a good idea. You'd use list(map(print, l)), to exhaust the map iterator, rather than printing the iterator itself. Commented Jun 8, 2014 at 21:14
  • @user2357112: If you don't actually care about the output of map() and just want to consume the iterator, you'd have to use something like consume = lambda iterator: deque(iterator, maxlen=0).
    – Blender
    Commented Jun 8, 2014 at 21:17
  • @Blender: If you're aware enough to avoid building a list, you're probably aware enough to not use map for this, though. Commented Jun 8, 2014 at 21:18
  • 1
    @KnightOfNi: Mostly, it comes down to the fact that map is not intended to be used for side effects, and people expect it not to have any. consume(map(print, l)) is fairly straightforward, not terribly bug-prone, and not potentially memory-intensive, but it generally doesn't offer any benefit over for x in l: print(x). More complex expressions in the map call grow unreadable, and whoever has to maintain your code probably won't like it. Commented Jun 8, 2014 at 21:45

4 Answers 4

9

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)
4
  • 1
    So, you're saying map() over for/in is more a stylistic choice?
    – KnightOfNi
    Commented Jun 8, 2014 at 21:13
  • @KnightOfNi: These days, pretty much. Note that map is for "build a sequence" or "apply a transformation" use cases; "make something happen" loops like printing the contents of a list are not map's role. Commented Jun 8, 2014 at 21:17
  • @KnightOfNi see also this topic: stackoverflow.com/questions/1247486/…
    – timgeb
    Commented Jun 8, 2014 at 21:22
  • @timgeb Thanks! I'm getting a definite impression that map() vs an iterator/generator is very stylistic.
    – KnightOfNi
    Commented Jun 8, 2014 at 21:30
2

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.

2
  • I was just about to post a comment saying that the first one seemed much cleaner. Nice anticipation. Anyway, would you say that map() is a better choice, as indicated by stackoverflow.com/questions/2573135/…
    – KnightOfNi
    Commented Jun 8, 2014 at 21:16
  • I used to think the for loop was cleaner as well. But 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. Commented Jun 8, 2014 at 21:18
1

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]
3
  • Yes, but that's not too much easier than simply using the above method.
    – KnightOfNi
    Commented Jun 8, 2014 at 21:12
  • At the end of the day, you can use for loops, list comprehensions, or maps for many of the same uses. It's up to you whatever you are comfortable with, is easy to read/understand, and is Pythonic! Commented Jun 8, 2014 at 21:16
  • OK, so there aren't any advantages at all? I read something about map() buffering one list with values of None to make it equal in length to another... so is it just better error handling and whatnot?
    – KnightOfNi
    Commented Jun 8, 2014 at 21:21
1

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.

2
  • I know what it does, but what besides the fact that it's shorter makes it BETTER? Also, your first program is so long as to be unrealistic - in fact, I don't think it even works.
    – KnightOfNi
    Commented Jun 8, 2014 at 23:51
  • Sorry, there was some errors indeed. It's not better, this is a another way to solve the same problem, applying a different programing paradigm, the functional. As Python is multi-paradigm, remove this features, could limit the features language to paradigm. Commented Jun 9, 2014 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.