0

I am having a hard time trying to convert a list of tuples, with key, values to a dictionary.

I have a list of tuples:

[('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
('season', '1', 'episode', '5', 'showkey', '1740'),
('season', '1', 'episode', '6', 'showkey', '1741'),
('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]

how can I make a dictionary with the key value pairs like:

{'season':1, 'episode':2, 'showkey':1736}

Thanks for any help!

Swatty43
  • 43
  • 2

5 Answers5

8

If you want one dictionary for each tuple:

>>> i = ('season', '1', 'episode', '2', 'showkey', '1737')
>>> dict(zip(i[::2], i[1::2]))
{'season': '1', 'episode': '2', 'showkey': '1737'}

To expand this for your list:

new_list = [dict(zip(i[::2], i[1::2])) for i in list_of_data]

This works by combining the following facts:

  1. The dict() constructor takes an iterable of pairs which it can convert to key/value.
  2. zip() will return tuples pairs from the two iterables passed to it.
  3. Slicing allows you add a starting offset, and a step argument.

Combining the above:

  1. First, we slice the tuple by odd positions i[::2] then by even positions, by skipping the first element i[1::2], this gives us two lists one representing the keys, the second the values:

    >>> i[::2]
    ('season', 'episode', 'showkey')
    >>> i[1::2]
    ('1', '2', '1737')
    
  2. Next, we feed these two lists to zip to give us (key,value) pairs:

    >>> zip(i[::2], i[1::2])
    [('season', '1'), ('episode', '2'), ('showkey', '1737')]
    
  3. Finally, we pass the resulting list to the dict() constructor:

    >>> dict([('season', '1'), ('episode', '2'), ('showkey', '1737')])
    {'season': '1', 'episode': '2', 'showkey': '1737'}
    
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • @Burhan thank you so much, just a quick follow up, why do I lose 'season':1 out of the very first tuple? `new_list[0]` `{'1': 'episode', 'showkey': '1736'}` – Swatty43 Aug 12 '15 at 12:44
  • Must be some syntax error in your code, when I run it against your sample code it works. – Burhan Khalid Aug 12 '15 at 13:22
1

I would use a list comprehension, and in that create dicts from the tuple by taking each 2 consecutive elements in it.

Code -

d = [dict((i[j],i[j+1]) for j in range(0,len(i),2)) for i in lt]

Here lt is the list of tuples .

Example -

>>> lt = [('season', '1', 'episode', '1', 'showkey', '1736'),
... ('season', '1', 'episode', '2', 'showkey', '1737'),
... ('season', '1', 'episode', '3', 'showkey', '1738'),
... ('season', '1', 'episode', '4', 'showkey', '1739'),
... ('season', '1', 'episode', '5', 'showkey', '1740'),
... ('season', '1', 'episode', '6', 'showkey', '1741'),
... ('season', '1', 'episode', '7', 'showkey', '1742'),
... ('season', '1', 'episode', '8', 'showkey', '1743'),
... ('season', '1', 'episode', '9', 'showkey', '1744'),
... ('season', '1', 'episode', '10', 'showkey', '1745'),
... ('season', '1', 'episode', '11', 'showkey', '1746'),
... ('season', '1', 'episode', '12', 'showkey', '1747'),
... ('season', '1', 'episode', '13', 'showkey', '1748')]
>>>
>>> d= [dict((i[j],i[j+1]) for j in range(0,len(i),2)) for i in lt]
>>> d
[{'showkey': '1736', 'episode': '1', 'season': '1'}, {'showkey': '1737', 'episode': '2', 'season': '1'}, {'showkey': '1738', 'episode': '3', 'season': '1'}, {'showkey': '1739', 'episode': '4', 'season': '1'}, {'showkey': '1740', 'episode':
 'season': '1'}, {'showkey': '1744', 'episode': '9', 'season': '1'}, {'showkey': '1745', 'episode': '10', 'season': '1'}, {'showkey': '1746', 'episode': '11', 'season': '1'}, {'showkey': '1747', 'episode': '12', 'season': '1'}, {'showkey':
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • I asked this question a few days ago, and this is a very similar question asked in this thread: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python – dyao Aug 12 '15 at 04:56
0
map(lambda datum: dict(zip(datum[::2], datum[1::2])), data)

In slow-mo:

results = []
for datum in data:
    # pick off every alternate item from the tuple, starting with the zeroth.
    keys = datum[::2]
    # pick off every alternate item from the tuple, starting with the oneth.
    values = datum[1::2]
    kv = zip(keys, values)
    new_dict = dict(kv)
    results.append(new_dict)

Please excuse typos, typing this on my phone.

Danver Braganza
  • 1,295
  • 10
  • 10
0
x=[('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
 ('season', '1', 'episode', '5', 'showkey', '1740'),
  ('season', '1', 'episode','6', 'showkey', '1741'),
 ('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]
d={}
m=0
for i in x:
    d1={}
    for j in i[::2]:

        d1[j]=i[i.index(j)+1]

    d[m]=d1
    m=m+1

print d

You can create dictionary of dictionary to save all data.

vks
  • 67,027
  • 10
  • 91
  • 124
0
inputd = [('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
('season', '1', 'episode', '5', 'showkey', '1740'),
('season', '1', 'episode', '6', 'showkey', '1741'),
('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]


>>> map(dict, map(lambda x: zip(x[::2],x[1::2]), inputd))
[{'season': '1', 'episode': '1', 'showkey': '1736'}, {'season': '1', 'episode': '2', 'showkey': '1737'}, {'season': '1', 'episode': '3', 'showkey': '1738'}, {'season': '1', 'episode': '4', 'showkey': '1739'}, {'season': '1', 'episode': '5', 'showkey': '1740'}, {'season': '1', 'episode': '6', 'showkey': '1741'}, {'season': '1', 'episode': '7', 'showkey': '1742'}, {'season': '1', 'episode': '8', 'showkey': '1743'}, {'season': '1', 'episode': '9', 'showkey': '1744'}, {'season': '1', 'episode': '10', 'showkey': '1745'}, {'season': '1', 'episode': '11', 'showkey': '1746'}, {'season': '1', 'episode': '12', 'showkey': '1747'}, {'season': '1', 'episode': '13', 'showkey': '1748'}]

About python slice

  1. x[::2] - means start through end, skipping ahead 2 places each time
  2. x[1::2] - means, start on index 1 through end, skipping ahead 2 places each time
Suku
  • 3,820
  • 1
  • 21
  • 23