Some examples of the map function (Python)

vegaseat 0 Tallied Votes 434 Views Share

The map() function applies a function to every member of a list. Here a list of numbers 32 to 255 is changed to a list of the corresponding ASCII characters. Then map() is used in a more complex application to combine the two lists to form a dictionary containing number:character pairs.

# build a dictionary that maps the ordinals from 32 to 255
# to their ASCII character equivalents  eg. 33: '!'
# (note that 32 and 160 are spaces)
# Python24  by  vegaseat    10may2005

import operator

print "ASCII values of characters:"
print '-'*27  # print 27 dashes, cosmetic

# create an empty dictionary
charD = {}
# set keys from 32 to 255
keyList = range(32, 256)
# use map() to create the character list
# applies function chr() to every item of the list
valueList = map(chr, keyList)
# use map() to form a dictionary from the two lists
map(operator.setitem, [charD]*len(keyList), keyList, valueList)

print "%6s%6s%6s" % ("hex", "dec", "char")
# print the dictionary one associated pair each line
for key,value in charD.items():
    print "%6x%6d%6c" % (key, key, value)

print '-'*27
delocalizer 0 Newbie Poster

Using [charD]*len() as an argument to map() is clever, but in this context is a very poor "example of the map function" because it is so unPythonic.
In 2.4 is better (and simpler) to do:

charD = dict([ (n, chr(n)) for n in range(32, 256) ])

replacing five lines (including the import ) with one, and a simple one at that. Perhaps a better title for this snippet would be 'an example of functional programming style in Python'.

Gribouillis 1,391 Programming Explorer Team Colleague

Using [charD]*len() as an argument to map() is clever, but in this context is a very poor "example of the map function" because it is so unPythonic.
In 2.4 is better (and simpler) to do:

charD = dict([ (n, chr(n)) for n in range(32, 256) ])

replacing five lines (including the import ) with one, and a simple one at that. Perhaps a better title for this snippet would be 'an example of functional programming style in Python'.

I think this is unfair: before list comprehensions became the rule, using map was considered a very pythonic way to do. You should better ask Guido Van Rossum why he didn't invent list comprehensions earlier. The language changes slowly and there is no point in reviving old snippets just to say that they are poor programming style.
Furthermore, map exists in many functional and non functional languages (lisp, ocaml, haskell, perl ...) and at a time, it sounded like a quite natural feature in python.

delocalizer 0 Newbie Poster

Two points:
1) The reason for my comment was principally because this is the number 6 hit on Google search for examples of map() in Python, and the first hit not from the actual language documentation. The comment is a comment for the benefit of current readers. Anyone not familiar with the language might think this was the right way to go to build the dictionary in the example, and it just isn't (and hasn't been for a long time - see below). It might be the right way to go if you had different objectives in mind, but in the case of the example, it is obtuse.
2) I completely agree that it's quite pointless reviewing old snippets after the language has changed, but list comprehensions have been part of the language since 2.0, released 10 years ago now - and 5 years before the initial snippet (using 2.4)!

Gribouillis 1,391 Programming Explorer Team Colleague

Note that list comprehensions in python come from haskell, so they are also an example of functional programming in python. See this page http://wiki.python.org/moin/PythonVsHaskell for more about python and haskell.

It's true that list comprehension appeared in python 2.0, but it took some time before people considered it to be the norm. Many of us were used to 'map'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.