Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting list of lists into a dictionary of dictionaries in Python

Tags:

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?

like image 516
New2coding Avatar asked Sep 05 '18 13:09

New2coding


People also ask

How do I convert a list to a dictionary in Python?

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.

Is it possible to declare a list of dictionary and dictionary of list in Python?

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.


1 Answers

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  

Output

{'BM': {'Holesovice': {'Butterfly': 1600}},  'PP': {'E-shop': {'Dresses': 1500},         'Holesovice': {'Ear-rings': 2000, 'Skirts': 1000}}} 

Generalization

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.

like image 195
Olivier Melançon Avatar answered Oct 05 '22 10:10

Olivier Melançon