0

The piece of code below behaves differently on Python 3 than Python 2, and I couldn't find the reason behind it.

import string
var1 = [i for i in string.ascii_lowercase]
var2 = [i for i in range(1, 27)]
alphabet_map = {}
map(lambda x, y: alphabet_map.update({x:y}), var2, var1)
print(alphabet_map)

Running the above code on Python 2 prints:

{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}

while on Python 3 it prints: {}

I am aware that I could have used a loop after both var1 and var2 or some other way to achieve what I want, but why is there a difference between the behavior of map function?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
InvisibleWolf
  • 917
  • 1
  • 9
  • 22

0 Answers0