2

I have two dictionaries:

dic_of_dics1 = {1: {"a": "A"}, 2: {"a": "B"}, 3: {"a": "C"}}
dic_of_dics2 = {1: {"d": "20"}, 2: {"d": "30"}, 3: {"d": "40"}}

I want to enumerate over the values of the dictionaries simultaneously; similar to this: Enumerate two python lists simultaneously? :

for index, (value1, value2) in enumerate(zip(dic_of_dics1, dic_of_dics2)):
print (value1, value2, subkey1, subkey2, index) 

Desired output:

A, 20, a, d, 0
B, 30, a, d, 1
C, 40, a, d, 2
Leo
  • 1,176
  • 1
  • 13
  • 33

4 Answers4

2

For your simplified case:

dic_of_dics1 = {1: {"a": "A"}, 2: {"a": "B"}, 3: {"a": "C"}}
dic_of_dics2 = {1: {"d": "20"}, 2: {"d": "30"}, 3: {"d": "40"}}

for val1, val2 in zip(dic_of_dics1.values(), dic_of_dics2.values()):
    print('{}, {}'.format(list(val1.values())[0], list(val2.values())[0]))

The output:

A, 20
B, 30
C, 40

Update (even shorter):

for i, (d1, d2) in enumerate(zip(dic_of_dics1.values(), dic_of_dics2.values())):
    (k1, v1), (k2, v2) = *d1.items(), *d2.items()
    print(v1, v2, k1, k2, i, sep=', ')

The output:

A, 20, a, d, 0
B, 30, a, d, 1
C, 40, a, d, 2
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • 1
    would this also be correct : for index, (val1, val2) in enumerate zip(dic_of_dics1.values(), dic_of_dics2.values()) cause I also need the index – Leo Jun 21 '19 at 10:32
  • 1
    @David8, *cause I also need the index* - Reflect how the index is supposed to be used in your question. How it relates to the output in question? – RomanPerekhrest Jun 21 '19 at 10:39
  • I've added an edit but maybe it is too long and takes away from the original question? should I keep it or move it to a new question? – Leo Jun 21 '19 at 11:15
1

One more readable and robust version by unpack and sorted, and add index:

dic_of_dics1 = {2: {"a": "B"}, 1: {"a": "A"}, 3: {"a": "C"}}
dic_of_dics2 = {1: {"d": "20"}, 2: {"d": "30"}, 3: {"d": "40"}}

for i, k in enumerate(sorted(dic_of_dics1)):
    dic1, dic2 = dic_of_dics1[k], dic_of_dics2[k]
    (k_a,), (a,), (k_b,), (b,) = dic1.keys(), dic1.values(), dic2.keys(), dic2.values()
    print(', '.join([a, b, k_a, k_b, str(i)]))

output:

A, 20, a, d, 0
B, 30, a, d, 1
C, 40, a, d, 2

I think depending on the order of dict's key is a bad idea.

LiuXiMin
  • 1,225
  • 8
  • 17
  • I like this format, is there a way to also extract the second 'key' Eg.("a", "d"). I use the index to iterate over tb = widgets.TabBar(list(dicimagename.keys())) in my original code, I simplified it here cause my original code is really long – Leo Jun 21 '19 at 11:37
0

Try this.

dic_of_dics1 = {1: {"a": "A"}, 2: {"a": "B"}, 3: {"a": "C"}}
dic_of_dics2 = {1: {"d": "20"}, 2: {"d": "30"}, 3: {"d": "40"}}

new_dict = {}
for i in dic_of_dics1:
    new_dict[list(dic_of_dics1[i].values())[0]] = list(dic_of_dics2[i].values())[0]
print(new_dict)

OR

enumerate with zip- iterate over two lists and their indices using enumerate together with zip

new_dict={}

for index,(value1, value2) in enumerate(zip(dic_of_dics1.values(), dic_of_dics2.values())):
    new_dict[list(value1.values())[0]] = list(value2.values())[0]

print(new_dict)

O/P:

{'A': '20', 'B': '30', 'C': '40'}
bharatk
  • 4,202
  • 5
  • 16
  • 30
0

An optional one-liner:

print('\n'.join([', '.join([list(x.values())[0], list(y.values())[0]]) for x, y in zip(dic_of_dics1.values(), dic_of_dics2.values())]))

Output:

A, 20
B, 30
C, 40
U13-Forward
  • 69,221
  • 14
  • 89
  • 114