Better way will be to use itertool.izip_longest()
with list comprehension as it will place value as None
in case elements are odd in number. For example:
>>> from itertools import izip_longest
>>> odd_list = ["a", "b", "c"]
>>> [{'key': k, 'value': v} for k, v in izip_longest(odd_list[::2], odd_list[1::2])]
[{'value': 'b', 'key': 'a'}, {'value': None, 'key': 'c'}]
# ^ Value as None
However if it is safe to assume even count of elements, you may use zip()
as well as:
>>> my_list = ["a","b","c","d","e","f"]
>>> [{'key': k, 'value': v} for k, v in zip(my_list[::2], my_list[1::2])]
[{'value': 'b', 'key': 'a'}, {'value': 'd', 'key': 'c'}, {'value': 'f', 'key': 'e'}]