Make dictionary from list with python
Posted
by prosseek
on Stack Overflow
See other posts from Stack Overflow
or by prosseek
Published on 2010-04-08T02:20:43Z
Indexed on
2010/04/08
2:23 UTC
Read the original article
Hit count: 331
I need to transform a list into dictionary as follows. The odd elements has the key, and even number elements has the value.
x = (1,'a',2,'b',3,'c') -> {1: 'a', 2: 'b', 3: 'c'}
def set(self, val_):
i = 0
for val in val_:
if i == 0:
i = 1
key = val
else:
i = 0
self.dict[key] = val
My solution seems to long, is there a better way to get the same results?
© Stack Overflow or respective owner