Why can a python dictionary be modified in the variable of a list comprehension?

53 Views Asked by At

From an amazing (and evil) video about the Oneliner-izer, I learned that a Python dictionary can be modified in the variable of a list comprehension. I tried to run the following code:

some_dict = dict()
some_dict['x'] = 1
return_value = [999 for some_dict['x'] in [42]][0]

It is clear to me that return_value should now be 999, but why should some_dict['x'] now be 42? How is the list comprehension syntax even legal? Let alone modifying some_dict?

0

There are 0 best solutions below