Python – Understanding Lambda

lambdamappython

X = 5
L = list(map(lambda x: 2**X, range(7)))
print (L)

… I'm expecting this to return:

[1, 2, 4, 8, 16, 32, 64]

…instead, it returns:

[32, 32, 32, 32, 32, 32, 32]

What am I doing wrong?

Best Solution

Python is case-sensitive, so lambda x: 2**X means: take an argument, call it (lowercase) x, ignore it completely, and return 2 to the power of global variable (uppercase) X.