NameError when assigning dictionary values

Gordon McMillan gmcm at hypernet.com
Sun Apr 2 17:43:33 EDT 2000


lewst writes:

> This simple problem is driving my crazy.  I can't figure out why I get
> a NameError with this program.
> 
>   # add each key-value pair to the dictionary and
>   # then for each value, create an empty list.
>   mydict = {}                         
>   for key,value in [ (".", "one"), ("+", "two") ]:
>       mydict[key] = value
>       value = []
>   print mydict
>   print one,two
> 
> Whe I run this I get "NameError: one".  Why aren't my 2 empty lists
> named "one" and "two" being created?

You're creating two empty lists, both named "value".The first 
is thown away by creating the second. Nowhere are "one" and 
"two" variable names, hence the NameError. 

- Gordon




More information about the Python-list mailing list