25

I have the below code which currently just prints the values of the initial dictionary. However I would like to iterate through every key of the nested dictionary to initially just print the names. Please see my code below:

Liverpool = {
    'Keepers':{'Loris Karius':1,'Simon Mignolet':2,'Alex Manninger':3},
    'Defenders':{'Nathaniel Clyne':3,'Dejan Lovren':4,'Joel Matip':5,'Alberto Moreno':6,'Ragnar Klavan':7,'Joe Gomez':8,'Mamadou Sakho':9}
}

for k,v in Liverpool.items():
    if k =='Defenders':
       print(v)
Shaido
  • 27,497
  • 23
  • 70
  • 73
Krishn
  • 813
  • 3
  • 14
  • 28
  • Do you want only the deepest key, value pairs to be printed, i.e. the peoples names and their corresponding number or also this key, value pair for example: `'Keepers', {'Loris Karius':1,'Simon Mignolet':2,'Alex Manninger':3}` – timakro Aug 30 '16 at 17:49
  • Can the nested tree be deeper than your example? – timakro Aug 30 '16 at 17:52
  • You can use the comprehension list: names = [ name for player in Liverpool.values() for name in player.keys() ] – Monica Aug 30 '16 at 18:18

3 Answers3

62

In other answers, you were pointed to how to solve your task for given dicts, with maximum depth level equaling to two. Here is the program that will alows you to loop through key-value pair of a dict with unlimited number of nesting levels (more generic approach):

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield from recursive_items(value)
        else:
            yield (key, value)

a = {'a': {1: {1: 2, 3: 4}, 2: {5: 6}}}

for key, value in recursive_items(a):
    print(key, value)

Prints

1 2
3 4
5 6

That is relevant if you are interested only in key-value pair on deepest level (when value is not dict). If you are also interested in key-value pair where value is dict, make a small edit:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield (key, value)
            yield from recursive_items(value)
        else:
            yield (key, value)

a = {'a': {1: {1: 2, 3: 4}, 2: {5: 6}}}

for key, value in recursive_items(a):
    print(key, value)

Prints

a {1: {1: 2, 3: 4}, 2: {5: 6}}
1 {1: 2, 3: 4}
1 2
3 4
2 {5: 6}
5 6
Dmitry Torba
  • 3,004
  • 1
  • 14
  • 24
12

Here is code that would print all team members:

for k, v in Liverpool.items():
    for k1, v1 in v.items():
        print(k1)

So you just iterate every inner dictionary one by one and print values.

Shaido
  • 27,497
  • 23
  • 70
  • 73
Nikolay Zinov
  • 308
  • 2
  • 7
  • 17
    This answer doesn't check whether the inner levels are dictionaries themselves and would result in an error if otherwise. – gented Nov 06 '19 at 15:23
1
Liverpool = {
    'Keepers':{'Loris Karius':1,'Simon Mignolet':2,'Alex Manninger':3},
    'Defenders':{'Nathaniel Clyne':3,'Dejan Lovren':4,'Joel Matip':5,'Alberto Moreno':6,'Ragnar Klavan':7,'Joe Gomez':8,'Mamadou Sakho':9}
}

for k,v in Liverpool.items():
    print(v.keys())

Yields:

['Alberto Moreno', 'Joe Gomez', 'Dejan Lovren', 'Ragnar Klavan', 'Joel Matip', 'Nathaniel Clyne', 'Mamadou Sakho']
['Alex Manninger', 'Loris Karius', 'Simon Mignolet']
Kevin London
  • 4,628
  • 1
  • 21
  • 29