How to create a dictionary containing lambda expressions using comprehension list?
Question:
I am trying to generate (something like) the following dictionary:
funcs1 = {
'0':lambda x_x==0,
'1':lambda x_x==1,
'2':lambda x_x==2,
'3':lambda x_x==3,
'4':lambda x_x==4,
'5':lambda x_x==5,
}
I tried to create the dictionary with a list comprehension like this:
funcs2 = {str(i):lambda x_x==i for i in range(0,6)}
Or simply using a for loop:
funcs3 = {}
for i in range(0,6):
funcs3.update({str(i): lambda x_x==i})
However, funcs2
and funcs3
are not the same as funcs1
, for example, when calling the element '0'
of each one of them and applying it to 0
, the results are different:
funcs1['0'](0)
Out[2]: True
funcs2['0'](0)
Out[3]: False
funcs3['0'](0)
Out[4]: False
Can somebody please help me out and point out where I am making a mistake?
Answers:
This is a common misunderstanding caused by Python late binding, this should fix your code:
funcs1 = {
'0': lambda x: x == 0,
'1': lambda x: x == 1,
'2': lambda x: x == 2,
'3': lambda x: x == 3,
'4': lambda x: x == 4,
'5': lambda x: x == 5,
}
funcs2 = {str(i): lambda x, i=i: x == i for i in range(0, 6)}
funcs3 = {}
for i in range(0, 6):
funcs3.update({str(i): lambda x, i=i: x == i})
print(funcs1['0'](0))
print(funcs2['0'](0))
print(funcs3['0'](0))
Output
True
True
True
This works:
>>> def make_fn(i):
... def fn(x):
... return x==i
... return fn
...
>>> funcs = {str(i): make_fn(i) for i in range(6)}
Testing, I get:
>>> funcs['0'](0)
True
>>> funcs['1'](1)
True
>>> funcs['0'](1)
False
I am trying to generate (something like) the following dictionary:
funcs1 = {
'0':lambda x_x==0,
'1':lambda x_x==1,
'2':lambda x_x==2,
'3':lambda x_x==3,
'4':lambda x_x==4,
'5':lambda x_x==5,
}
I tried to create the dictionary with a list comprehension like this:
funcs2 = {str(i):lambda x_x==i for i in range(0,6)}
Or simply using a for loop:
funcs3 = {}
for i in range(0,6):
funcs3.update({str(i): lambda x_x==i})
However, funcs2
and funcs3
are not the same as funcs1
, for example, when calling the element '0'
of each one of them and applying it to 0
, the results are different:
funcs1['0'](0)
Out[2]: True
funcs2['0'](0)
Out[3]: False
funcs3['0'](0)
Out[4]: False
Can somebody please help me out and point out where I am making a mistake?
This is a common misunderstanding caused by Python late binding, this should fix your code:
funcs1 = {
'0': lambda x: x == 0,
'1': lambda x: x == 1,
'2': lambda x: x == 2,
'3': lambda x: x == 3,
'4': lambda x: x == 4,
'5': lambda x: x == 5,
}
funcs2 = {str(i): lambda x, i=i: x == i for i in range(0, 6)}
funcs3 = {}
for i in range(0, 6):
funcs3.update({str(i): lambda x, i=i: x == i})
print(funcs1['0'](0))
print(funcs2['0'](0))
print(funcs3['0'](0))
Output
True
True
True
This works:
>>> def make_fn(i):
... def fn(x):
... return x==i
... return fn
...
>>> funcs = {str(i): make_fn(i) for i in range(6)}
Testing, I get:
>>> funcs['0'](0)
True
>>> funcs['1'](1)
True
>>> funcs['0'](1)
False