3

I have this list:

single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']

What's the best way to create a dictionary from this?

Thanks.

zachwill
  • 4,395
  • 3
  • 19
  • 17
  • 1
    duplicate, I can clearly remember exactly the same question. a while ago. – SilentGhost Oct 26 '10 at 17:15
  • How do you want to handle duplicate keys? – Sam Dolan Oct 26 '10 at 17:17
  • 2
    If you built this list, the form `[('key','value'),...]` would be preferable, then you just need to pass it to `dict()` – Nick T Oct 26 '10 at 17:18
  • @SilentGhost possible duplicate of [Converting a single ordered list in python to a dictionary, pythonically](http://stackoverflow.com/questions/1639772/converting-a-single-ordered-list-in-python-to-a-dictionary-pythonically) – Josh Lee Oct 26 '10 at 19:54

6 Answers6

11
>>> single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
>>> dict(zip(single[::2], single[1::2]))
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 3
    `timeit`'d it, runs 2 times faster than a dict comprehension inspired by pyfunc's answer (which in turn beats a list-of-tuple comprehension) o.O +1 since it's also the simplest solution. –  Oct 26 '10 at 17:22
8

Similar to SilentGhost's solution, without building temporary lists:

>>> from itertools import izip
>>> single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
>>> si = iter(single)
>>> dict(izip(si, si))
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
pillmuncher
  • 10,094
  • 2
  • 35
  • 33
2

This is the simplest I guess. You will see more wizardry in solution here using list comprehension etc

dictObj = {}
for x in range(0, len(single), 2):
    dictObj[single[x]] = single[x+1]

Output:

>>> single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
>>> dictObj = {}
>>> for x in range(0, len(single), 2):
...     dictObj[single[x]] = single[x+1]
... 
>>> 
>>> dictObj
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
>>> 
pyfunc
  • 65,343
  • 15
  • 148
  • 136
0
L = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
d = dict(L[n:n+2] for n in xrange(0, len(L), 2))
nosklo
  • 217,122
  • 57
  • 293
  • 297
0
>>> single = ['key', 'value', 'key2', 'value2', 'key3', 'value3']
>>> dict(zip(*[iter(single)]*2))
{'key3': 'value3', 'key2': 'value2', 'key': 'value'}

Probably not the most readable version though ;)

adw
  • 4,901
  • 1
  • 25
  • 18
0

You haven't specified any criteria for "best". If you want understandability, simplicity, easily modified to check for duplicates and odd number of inputs, works with any iterable (in case you can't find out the length in advance), NO EXTRA MEMORY USED, ... try this:

def load_dict(iterable):
    d = {}
    pair = False
    for item in iterable:
        if pair:
            # insert duplicate check here
            d[key] = item
        else:
            key = item
        pair = not pair
    if pair:
        grumble_about_odd_length(key)
    return d
John Machin
  • 81,303
  • 11
  • 141
  • 189