help w/assigning lists to dict

Preston Landers prestonlanders at my-deja.com
Thu Nov 18 20:54:12 EST 1999


Greetings,

I don't know what the 'official' way is, but this works for me:

my_list = ['key1','val1','key2','val2']
my_dict = {}

for key in range(0, len(my_list), 2):
    my_dict[ my_list[ key ] ] = my_list[ key + 1 ]

There may be some function call to accomplish same, but this seems like
the most direct approach.  AFAIK there is no built in semantics
equivelent to Perl's assign-list-to-hash.

---Preston Landers


In article <8125b9$kuf$1 at nnrp1.deja.com>,
  nielsenjf at my-deja.com wrote:
>
>
> Hello there, this is hopefully a simple question to answer.
>
> If I have, a list of key,value pairs, is there any way to just assign
it
> to a dictionary? I'm thinking of the case in perl, where you can
assign
> a list to an assoc array.
>
> For example:
>
> a=('key1','val1','key2','val2')
> b={ a } ???????????
>
> Is there a common python idiom for this?

--
|| Preston Landers <prestonlanders at my-deja.com> ||


Sent via Deja.com http://www.deja.com/
Before you buy.




More information about the Python-list mailing list