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?