List Of Dicts To/from Dict Of Lists
Solution 1:
For those of you that enjoy clever/hacky one-liners.
Here is DL
to LD
:
v = [dict(zip(DL,t)) for t in zip(*DL.values())]
print(v)
and LD
to DL
:
v = {k: [dic[k] for dic in LD] for k in LD[0]}
print(v)
LD
to DL
is a little hackier since you are assuming that the keys are the same in each dict
. Also, please note that I do not condone the use of such code in any kind of real system.
Solution 2:
Perhaps consider using numpy:
import numpy as np
arr = np.array([(0, 2), (1, 3)], dtype=[('a', int), ('b', int)])
print(arr)
# [(0, 2) (1, 3)]
Here we access columns indexed by names, e.g. 'a'
, or 'b'
(sort of like DL
):
print(arr['a'])
# [0 1]
Here we access rows by integer index (sort of like LD
):
print(arr[0])
# (0, 2)
Each value in the row can be accessed by column name (sort of like LD
):
print(arr[0]['b'])
# 2
Solution 3:
If you're allowed to use outside packages, Pandas works great for this:
import pandas as pd
pd.DataFrame(DL).to_dict(orient="records")
Which outputs:
[{'a': 0, 'b': 2}, {'a': 1, 'b': 3}]
You can also use orient="list"
to get back the original structure
{'a': [0, 1], 'b': [2, 3]}
Solution 4:
To go from the list of dictionaries, it is straightforward:
You can use this form:
DL={'a':[0,1],'b':[2,3], 'c':[4,5]}
LD=[{'a':0,'b':2, 'c':4},{'a':1,'b':3, 'c':5}]
nd={}
for d in LD:
for k,v in d.items():
try:
nd[k].append(v)
except KeyError:
nd[k]=[v]
print nd
#{'a': [0, 1], 'c': [4, 5], 'b': [2, 3]}
Or use defaultdict:
nd=cl.defaultdict(list)
for d in LD:
for key,val in d.items():
nd[key].append(val)
print dict(nd.items())
#{'a': [0, 1], 'c': [4, 5], 'b': [2, 3]}
Going the other way is problematic. You need to have some information of the insertion order into the list from keys from the dictionary. Recall that the order of keys in a dict is not necessarily the same as the original insertion order.
For giggles, assume the insertion order is based on sorted keys. You can then do it this way:
nl=[]
nl_index=[]
for k in sorted(DL.keys()):
nl.append({k:[]})
nl_index.append(k)
for key,l in DL.items():
for item in l:
nl[nl_index.index(key)][key].append(item)
print nl
#[{'a': [0, 1]}, {'b': [2, 3]}, {'c': [4, 5]}]
If your question was based on curiosity, there is your answer. If you have a real-world problem, let me suggest you rethink your data structures. Neither of these seems to be a very scalable solution.
Solution 5:
Here are the one-line solutions (spread out over multiple lines for readability) that I came up with:
if dl is your original dict of lists:
dl = {"a":[0, 1],"b":[2, 3]}
Then here's how to convert it to a list of dicts:
ld = [{key:value[index] for key,value in dl.items()}
for index in range(max(map(len,dl.values())))]
Which, if you assume that all your lists are the same length, you can simplify and gain a performance increase by going to:
ld = [{key:value[index] for key, value in dl.items()}
for index in range(len(dl.values()[0]))]
If dl
contains unsymmetrical lists the following works fine:
from itertools import product
dl = {"a":[0, 1],"b":[2, 3, 4], "c":[5, 6, 7, 8]}
ld = [dict(zip(dl.keys(), items))
for items in product(*(dl.values()))]
Here's how to convert that back into a dict of lists:
dl2 = {key:[item[key] for item in ld]
for key in list(functools.reduce(
lambda x, y: x.union(y),
(set(dicts.keys()) for dicts in ld)
))
}
If you're using Python 2 instead of Python 3, you can just use reduce
instead of functools.reduce
there.
You can simplify this if you assume that all the dicts in your list will have the same keys:
dl2 = {key:[item[key] for item in ld] for key in ld[0].keys() }
Post a Comment for "List Of Dicts To/from Dict Of Lists"