List of dicts to/from dict of lists
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
List of dicts to/from dict of lists, but with all possible permutations
Make sure every value is a list and then use itertools.product
:
from itertools import product
# DL = {'a': [0, 1], 'b': [2, 3]}
D = {'a': [0, 1], 'b': [2, 3], 'c': 7, 'd': 9}
res = [dict(zip(D, p)) for p in product(*[v if isinstance(v, list) else [v] for v in D.values()])]
print(res)
Output
[{'a': 0, 'b': 2, 'c': 7, 'd': 9}, {'a': 0, 'b': 3, 'c': 7, 'd': 9}, {'a': 1, 'b': 2, 'c': 7, 'd': 9}, {'a': 1, 'b': 3, 'c': 7, 'd': 9}]
Converting a dictionary with lists into a list of dictionaries
Using zip
:
[dict(zip(d, vals)) for vals in zip(*d.values())]
Result:
[{'n': 'a', 'a': 1, 'p': '123'}, {'n': 'b', 'a': 2, 'p': '321'}, {'n': 'x', 'a': 3, 'p': '456'}]
How do I convert a list of dictionaries to a dictionary of lists in Python?
import collections
result = collections.defaultdict(list)
for d in dictionaries:
for k, v in d.items():
result[k].append(v)
Most pythonic way to convert a dict of lists into a list of dicts of all combs of the elements in the lists from the dict?
You could use itertools.product
on just the values of the dict and then dict(zip(...))
the keys and values together to output a list of dicts.
from itertools import product
d = {'Apple':[3,5,7],'Pear':[5,2],'Orange':[10],'Banana':[7,10],'Lemon':[5,7,9,2,1]}
l = [dict(zip(d.keys(), vals)) for vals in product(*d.values())]
print(l)
# [{'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 5}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 7}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 9}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 2}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 1}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 5}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 7}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 9}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 2}, {'Apple': 3, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 1}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 5}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 7}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 9}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 2}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 1}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 5}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 7}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 9}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 2}, {'Apple': 3, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 1}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 5}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 7}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 9}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 2}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 1}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 5}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 7}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 9}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 2}, {'Apple': 5, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 1}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 5}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 7}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 9}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 2}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 1}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 5}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 7}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 9}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 2}, {'Apple': 5, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 1}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 5}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 7}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 9}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 2}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 7, 'Lemon': 1}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 5}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 7}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 9}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 2}, {'Apple': 7, 'Pear': 5, 'Orange': 10, 'Banana': 10, 'Lemon': 1}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 5}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 7}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 9}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 2}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 7, 'Lemon': 1}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 5}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 7}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 9}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 2}, {'Apple': 7, 'Pear': 2, 'Orange': 10, 'Banana': 10, 'Lemon': 1}]
Split dictionary of lists into list of dictionaries
Works for any number of keys
>>> map(dict, zip(*[[(k, v) for v in value] for k, value in d.items()]))
[{'key2': 4, 'key1': 1}, {'key2': 5, 'key1': 2}, {'key2': 6, 'key1': 3}]
For example:
d = {'key3': [7, 8, 9], 'key2': [4, 5, 6], 'key1': [1, 2, 3]}
>>> map(dict, zip(*[[(k, v) for v in value] for k, value in d.items()]))
[{'key3': 7, 'key2': 4, 'key1': 1}, {'key3': 8, 'key2': 5, 'key1': 2}, {'key3': 9, 'key2': 6, 'key1': 3}]
A general solution that works on any number of values or keys: (python2.6)
>>> from itertools import izip_longest
>>> d = {'key2': [3, 4, 5, 6], 'key1': [1, 2]}
>>> map(lambda a: dict(filter(None, a)), izip_longest(*[[(k, v) for v in value] for k, value in d.items()]))
[{'key2': 3, 'key1': 1}, {'key2': 4, 'key1': 2}, {'key2': 5}, {'key2': 6}]
And if you don't have python2.6:
>>> d = {'key2': [3, 4, 5, 6], 'key1': [1, 2]}
>>> map(lambda a: dict(filter(None, a)), map(None, *[[(k, v) for v in value] for k, value in d.items()]))
[{'key2': 3, 'key1': 1}, {'key2': 4, 'key1': 2}, {'key2': 5}, {'key2': 6}]
How to convert list of dictionaries into list of lists
I find this site that use blew code:
How to convert a list of dictionaries to a list of lists in Python
[[row[key] for row in myList] for key in keylist]
Test:
def bycol_decl(lod, keylist):
return [[row[key] for row in lod] for key in keylist]
if __name__ == "__main__":
lod = [
{'a':1, 'b':2, 'c':3},
{'a':4, 'b':5, 'c':6},
{'a':7, 'b':8, 'c':9},
]
keylist = ['a', 'b', 'c']
print bycol_decl(lod, keylist)
Results:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Convert a list of lists to a dictionary
You have zip
as an option:
wanted = {a[0]: list(a[1:]) for a in zip(*x)}
Or if you're familiar with unpacking:
wanted = {k: v for k, *v in zip(*x)}
Related Topics
Get Name of Current Script in Python
How Does This Input Work with the Python 'Any' Function
What Is the Syntax to Insert One List into Another List in Python
Change the Colors Within Certain Range to Another Color Using Opencv
Full Examples of Using Pyserial Package
How to Distribute Python Programs
How to Generate Circular Thumbnails with Pil
Failed to Get Convolution Algorithm. This Is Probably Because Cudnn Failed to Initialize,
Return String with First Match for a Regex, Handling Case Where There Is No Match
Python Scope: "Unboundlocalerror: Local Variable 'C' Referenced Before Assignment"
Progress Indicator During Pandas Operations
How to Check If an Ip Is in a Network in Python
How to List Pip Dependencies/Requirements
Creating Spark Data Structure from Multiline Record
How to Change Plot Background Color
Matplotlib: Overlay Plots with Different Scales
Why Does Python Pep-8 Strongly Recommend Spaces Over Tabs for Indentation