list to map in python -


i have list , want convert list map

mylist = ["a",1,"b",2,"c",3] 

mylist equivalent

mylist = [key,value,key,value,key,value] 

so input:

mylist = ["a",1,"b",2,"c",3] 

output:

mymap = {"a":1,"b":2,"c":3} 

p.s: have written following function same work, want use iterator tools of python:

def fun():     mylist = ["a",1,"b",2,"c",3]     mymap={}     count = 0     value in mylist:         if not count%2:             mymap[value] = mylist[count+1]         count = count+1     return mymap         

using iter , dict-comprehension:

>>> mylist = ["a",1,"b",2,"c",3] >>> = iter(mylist) >>> {k: next(it) k in it} {'a': 1, 'c': 3, 'b': 2} 

using zip , iter:

>>> dict(zip(*[iter(mylist)]*2)) #use `itertools.izip` if list huge. {'a': 1, 'c': 3, 'b': 2} 

related: how zip(*[iter(s)]*n) work in python


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -