a clean way to define dictionary

Kendear kendear at nospam.com
Wed Jun 18 00:50:24 EDT 2003


Kendear wrote:
> 
> i hope to define a dictionary this way:
> 
> lst = """
> a    1
> foo  3
> bar  234
> joe  321
> """
> 
> lst = lst.split()
> 
> now lst refers to  ['a', '1', 'foo', '3', 'bar', '234', 'joe', '321']
> i want to do something like
> 
> dict = {}
> for key, value in lst:
>     dict[key] = eval(value)
> 
> 
> but key, value is not for taking 2
> items at a time, but take a tuple
> and unpacking it...
> 
> is there a way for the "for"
> to take 2 items at a time?
> 
> or is there a more common way to define a dictionary
> without all the punctuation marks?


of course, it can be done as

for i in range(0, len(lst), 2):
     dict[lst[i]] = eval(lst[i+1])

but is a little messy... is there a cleaner way?





More information about the Python-list mailing list