Python Map vs For in Python

#Python

Python Map vs For in Python

map is sometimes more convenient instead of for. The code

newlist = []
for word in oldlist:
    newlist.append(word.upper())

can be reformed using map

newlist = map(str.upper, oldlist)

for loop is sometimes slow because the dot evaluation inside is evaluated for each loop. Thus the following code is more efficient.

upper = str.upper
newlist = []
append = newlist.append
for word in oldlist:
    append(upper(word))

Published: by ;

Lei Ma (2015). 'Python Map vs For in Python', Datumorphism, 12 April. Available at: https://datumorphism.leima.is/til/programming/python/python-map-vs-for/.

Table of Contents

Current Ref:

  • til/programming/python/python-map-vs-for.md