I try to create a dictionary which will have a relations to different sides taken from the list.
import collections
def create_dict(sides = ["A", "B", "C"]):
my_dict = dict.fromkeys(sides, {})
print("my_dict created here: ", my_dict)
for k in sides:
remining_list = collections.deque(sides)
remining_list.remove(k)
my_dict[k]["relation"] = dict.fromkeys(remining_list, 0)
return my_dict
The dict is created with empty dictionaries:
('my_dict created here: ', {'A': {}, 'C': {}, 'B': {}})
I expect an output as:
{'A': {'relation': {'B': 0, 'C': 0}},
'B': {'relation': {'A': 0, 'C': 0}},
'C': {'relation': {'A': 0, 'B': 0}}}
but instead each value of inner dictionary comes to the last processed dictionary like this:
{'A': {'relation': {'A': 0, 'B': 0}},
'C': {'relation': {'A': 0, 'B': 0}},
'B': {'relation': {'A': 0, 'B': 0}}}
Can not figure out in which step I do wrong. When I loop over the keys I give it as a value only to this particular key not to all as in the output. Also is there any more efficient way to create this dictionary?
for completes this is example of call:
if __name__=="__main__":
my_dict = create_dict()
print(my_dict)