myDict = dict(zip(myList[::2], myList[1::2]))
Please do not use the βlistβ as the variable name, as it does not allow you to access the list () function.
If there is a lot of data, we can do this more efficiently using the iterator functions:
from itertools import izip, islice
myList = ['first_key', 'first_value', 'second_key', 'second_value']
myDict = dict(izip(islice(myList,0,None,2), islice(myList,1,None,2)))
source
share