Dictionary With Lambda Values Updates All Entries

This is a typical late binding issue (see common gotchas): when the functions (being lambda/anonymous has nothing to do with it) are called, they access the current value of item, which is the last one from the loop. Try

lambda x=item: x.data 

in your loop instead. This works since default arguments are bound to a function at definition time while common local variables are evaluated at calling time.

Similar (possible duplicate) question: Python Lambda in a loop

Leave a Comment