[Tutor] How to create a key-value pairs with alternative elementsin a list ... please help.

Jacob S. keridee at jayco.net
Thu Jan 13 02:33:59 CET 2005


> Problem:
> How do i capture every alternative element in list a:


Use Jeff Shannon's approach to get keys and vals.

> >>> keys = [] # create a list of all keys i.e a,b,c)
> >>> vals = [] # create a list of all values i.e 
>                #appele,boy,cat etc.
> 
> >>> dict = {}
> 
> >>> dict = zip(keys,vals)


What is that?!?
your dict thing ain't workin'
This is verified code... HA,HA,HA,Ha, ha, ha 
Well... okay, it's only tested, not verified.

fulllist = ['a','apple','b','boy','c','cat']
keys = fulllist[::2]
vals = fulllist[1::2]
dictionary = dict(zip(keys,vals))

HTH,
Jacob




More information about the Tutor mailing list