So I have a dict in a dict like so:
{'d': {'c': 3}, 'c': {'d': 3, 'b': 2}, 'b': {'c': 2, 'a': 1}, 'a': {'b': 1}}
And I need python code to give an output of :
("d","c")
("c","d")
("c","b")
("b","c")
("b","a")
("a","b")
I have no clue how to do this and any help is accepted.
Thank you
CodePudding user response:
You can use list comprehension with dict.items
:
dct = {'d': {'c': 3}, 'c': {'d': 3, 'b': 2}, 'b': {'c': 2, 'a': 1}, 'a': {'b': 1}}
output = [(key_1, key_2) for key_1, subdct in dct.items() for key_2 in subdct]
print(output) # [('d', 'c'), ('c', 'd'), ('c', 'b'), ('b', 'c'), ('b', 'a'), ('a', 'b')]
CodePudding user response:
x = {'d': {'c': 3}, 'c': {'d': 3, 'b': 2}, 'b': {'c': 2, 'a': 1}, 'a': {'b': 1}}
for i in x.keys():
for j in x.get(i).keys():
print((i,j))