I am trying to convert a list of lists data structure to a dictionary of dictionaries.
The list is defined as follows:
l = [ ['PP','Ear-rings', 'Holesovice', 2000], ['PP','Skirts', 'Holesovice', 1000], ['PP','Dresses', 'E-shop', 1500], ['BM','Butterfly', 'Holesovice', 1600] ]
My aim is to have the dictionary structure as follows:
#{'PP' : {'Holesovice' : {'Ear-rings' : 2000, 'Skirts' : 1000}, # 'E-shop' : {'Dresses' : 1500}}, # 'BM' : {'Holesovice' : {'Butterfly' : 1600}} #}
This bit of code does not return desired output:
labels_d = {} items_d = {} shops_d = {} for index, row in enumerate(l): items_d[row[1]] = row[3] shops_d[row[2]] = items_d labels_d[row[0]] = shops_d print(labels_d)
I found some posts that deal with converting lists to dictionaries here and here but I did not make it work the way I want. Is there any 'clean' way how to achieve the structure posted above?
Since python dictionary is unordered, the output can be in any order. To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.
In Python, you can have a List of Dictionaries. You already know that elements of the Python List could be objects of any type. In this tutorial, we will learn how to create a list of dictionaries, how to access them, how to append a dictionary to list and how to modify them.
Using dict.setdefault(key, {})
is a good way to approach the creation of nested dictionaries of fixed depth.
l = [ ['PP','Ear-rings', 'Holesovice', 2000], ['PP','Skirts', 'Holesovice', 1000], ['PP','Dresses', 'E-shop', 1500], ['BM','Butterfly', 'Holesovice', 1600] ] d = {} for tag, item, source, qty in l: d.setdefault(tag, {}).setdefault(source, {})[item] = qty
{'BM': {'Holesovice': {'Butterfly': 1600}}, 'PP': {'E-shop': {'Dresses': 1500}, 'Holesovice': {'Ear-rings': 2000, 'Skirts': 1000}}}
The above solution can be made more general by building a class of nested dictionary, dropping the requirements to have a fixed depth.
class NestedDict(dict): def __getitem__(self, item): if item not in self: self[item] = NestedDict() return super().__getitem__(item) d = NestedDict() for tag, item, source, qty in l: d[tag][source][item] = qty
Also notice that the class approach is created so it only creates an object if the key does not exist while the setdefault
approach created an empty dict
on every access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With