anwarshawn
anwarshawn

Reputation: 71

python list of tuples to a dictionary

I have a data format that looks like this:

for example,

[('2011', 1, 'value1', '68843'), 
 ('2011', 1, 'value2', '37029'),
 ('2014', 1, 'value1', '66061'),
 ('2014', 1, 'value2', '96994'),
 ('2016', 1, 'value1', '68286'),
 ('2016', 1, 'value2', '84507'), 
 ('2011', 1, 'value3', '58882')]

and I want to convert this data into the below format using python

[{"year":2011,"value1":68843,"value2":37029,"value3":58882}, 
 {"year":2014,"value1":66061,"value2":96994}, 
 {"year":2016,"value1":68286,"value2":84507}]

What is the syntactically cleanest way to accomplish this?

Upvotes: 0

Views: 81

Answers (3)

sanyassh
sanyassh

Reputation: 8550

import collections

values = [
 ('2011', 1, 'value1', '68843'), 
 ('2011', 1, 'value2', '37029'),
 ('2014', 1, 'value1', '66061'),
 ('2014', 1, 'value2', '96994'),
 ('2016', 1, 'value1', '68286'),
 ('2016', 1, 'value2', '84507'), 
 ('2011', 1, 'value3', '58882')
]

d = collections.defaultdict(dict)

for year, _, name, value in values:
    d[year][name] = value

result = [{'year': year, **values} for year, values in d.items()]
print(result)

Upvotes: 3

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6935

Try this :

a = [('2011', 1, 'value1', '68843'), ('2011', 1, 'value2', '37029'), ('2014', 1, 'value1', '66061'), ('2014', 1, 'value2', '96994'), ('2016', 1, 'value1', '68286'), ('2016', 1, 'value2', '84507'), ('2011', 1, 'value3', '58882')]
c =[]
for i in set([k[0] for k in a]):
    temp = {}
    temp['year'] =i
    for j in a:
        if j[0]==i:
            temp[j[2]] = int(j[-1])
    c.append(temp)

OUTPUT :

[{'year': '2011', 'value1': 68843, 'value2': 37029, 'value3': 58882}, {'year': '2014', 'value1': 66061, 'value2': 96994}, {'year': '2016', 'value1': 68286, 'value2': 84507}]

Upvotes: 2

Óscar López
Óscar López

Reputation: 236150

Here's an idea, in two passes:

from collections import defaultdict

ts = [('2011', 1, 'value1', '68843'), ('2011', 1, 'value2', '37029'),
      ('2014', 1, 'value1', '66061'), ('2014', 1, 'value2', '96994'),
      ('2016', 1, 'value1', '68286'), ('2016', 1, 'value2', '84507'),
      ('2011', 1, 'value3', '58882')]

# first, collect the data corresponding to a single year
d = defaultdict(list)
for year, _, val, num in ts:
    d[year].append((val, num))

# second, consolidate it in a list
[dict([['year', year]] + vals) for year, vals in d.items()]

=> [{'value2': '96994', 'value1': '66061', 'year': '2014'},
    {'value2': '84507', 'value1': '68286', 'year': '2016'},
    {'value3': '58882', 'value2': '37029', 'value1': '68843', 'year': '2011'}]

Upvotes: 1

Related Questions