dic1 ={'materials_id': 5, 'name': 'paint', 'capacity': 3}
dic2 ={'materials_id': 4, 'name': 'chemistry', 'capacity': 3}
become:
data ={ {'materials_id': 5, 'name': 'paint', 'capacity': 3}, {'materials_id': 4, 'name': 'chemistry', 'capacity': 3}}
CodePudding user response:
You can't hash a dictionary, so creating a set
of dictionaries is not possible.
Try creating a tuple
or list
instead:
>>> dic1 = {'materials_id': 5, 'name': 'paint', 'capacity': 3}
>>> dic2 = {'materials_id': 4, 'name': 'chemistry', 'capacity': 3}
>>> data = (dic1, dic2)
>>> data
({'materials_id': 5, 'name': 'paint', 'capacity': 3}, {'materials_id': 4, 'name': 'chemistry', 'capacity': 3})
>>> data = [dic1, dic2]
>>> data
[{'materials_id': 5, 'name': 'paint', 'capacity': 3}, {'materials_id': 4, 'name': 'chemistry', 'capacity': 3}]
Alternatively, you could utilize immutabledict
:
>>> from immutabledict import immutabledict
>>> data = {immutabledict(dic1), immutabledict(dic2)}
>>> data
{immutabledict({'materials_id': 4, 'name': 'chemistry', 'capacity': 3}), immutabledict({'materials_id': 5, 'name': 'paint', 'capacity': 3})}
CodePudding user response:
Maybe a different way to write that could be:
Data = { 'materials_id': id_indexlist[0] , 'name' : names_list[0], ...
Instead using a dictionary inside another dictionary you could combine a dictionary with several lists.