0

I have following nested dictionary

nested_dictionary = {
      "api": {
        "results": 4,
        "leagues": {
          "22": {
            "league_id": "22",
            "name": "Ligue 1",
            "country": "France",
            "season": "2017",
            "season_start": "2017-08-04",
            "season_end": "2018-05-19",
            "logo": "https://www.api-football.com/public/leagues/22.svg",
            "standings": True
          },
          "24": {
            "league_id": "24",
            "name": "Ligue 2",
            "country": "France",
            "season": "2017",
            "season_start": "2017-07-28",
            "season_end": "2018-05-11",
            "logo": "https://www.api-football.com/public/leagues/24.png",
            "standings": True
          },
          "157": {
            "league_id": "157",
            "name": "National",
            "country": "France",
            "season": "2017",
            "season_start": "2017-08-04",
            "season_end": "2018-05-11",
            "logo": "https://www.api-football.com/public/leagues/157.png",
            "standings": True
          },
          "206": {
            "league_id": "206",
            "name": "Feminine Division 1",
            "country": "France",
            "season": "2017",
            "season_start": "2017-09-03",
            "season_end": "2018-05-27",
            "logo": "https://www.api-football.com/public/leagues/206.png",
            "standings": True
          }
        }
      }
    }

i convert it to iterable with following code

dict_to_iterable = iter(nested_dictionary)

Now i am trying to iter it with following code

print(next(dict_to_iterable))
print(next(dict_to_iterable)) 

First statement return api but second give in console StopIteration. What i am doing wrong. Please help me

  • 1
    `next` advances the iterator by one element. `nested_dictionary` only has one element (key). – meowgoesthedog Feb 25 '19 at 14:00
  • 1
    What is your expected output? – Rakesh Feb 25 '19 at 14:03
  • I want to extract this data `"league_id": "206", "name": "Feminine Division 1", "country": "France", "season": "2017", "season_start": "2017-09-03", "season_end": "2018-05-27", "logo": "https://www.api-football.com/public/leagues/206.png", "standings": True` –  Feb 25 '19 at 14:08
  • `dict_to_iterable = iter(nested_dictionary["api"]["leagues"])` might get you closer. – tgikal Feb 25 '19 at 14:10
  • Could you add the expected output in the question, please? Is has much more visibility than in a comment. – Valentino Feb 25 '19 at 14:17

2 Answers2

0

nested_dictionary is having only one key i.e. "api".

If you were expecting to iterate over something else, you need to modify your code.

roschach
  • 8,390
  • 14
  • 74
  • 124
eiram_mahera
  • 950
  • 9
  • 25
0

You can create a function that check each item of a dictionary and if that item is not another dictionary then it can print the result. If the item is another dictionary then you can repeat the function for this layer.

def iterate_dictionary(d):
    for key in d.keys():
        # check if each item is dictionary
        if str(type(d[key])) == "<class 'dict'>":
            iterate_dictionary(d[key])
        else:
            print (key, d[key])

nested_dictionary = {
      "api": {
        "results": 4,
        "leagues": {
          "22": {
            "league_id": "22",
            "name": "Ligue 1",
            "country": "France",
            "season": "2017",
            "season_start": "2017-08-04",
            "season_end": "2018-05-19",
            "logo": "https://www.api-football.com/public/leagues/22.svg",
            "standings": True
          },
          "24": {
            "league_id": "24",
            "name": "Ligue 2",
            "country": "France",
            "season": "2017",
            "season_start": "2017-07-28",
            "season_end": "2018-05-11",
            "logo": "https://www.api-football.com/public/leagues/24.png",
            "standings": True
          },
          "157": {
            "league_id": "157",
            "name": "National",
            "country": "France",
            "season": "2017",
            "season_start": "2017-08-04",
            "season_end": "2018-05-11",
            "logo": "https://www.api-football.com/public/leagues/157.png",
            "standings": True
          },
          "206": {
            "league_id": "206",
            "name": "Feminine Division 1",
            "country": "France",
            "season": "2017",
            "season_start": "2017-09-03",
            "season_end": "2018-05-27",
            "logo": "https://www.api-football.com/public/leagues/206.png",
            "standings": True
          }
        }
      }
    }


iterate_dictionary(nested_dictionary)

This outputs:

results 4
league_id 22
name Ligue 1
country France
season 2017
season_start 2017-08-04
season_end 2018-05-19
logo https://www.api-football.com/public/leagues/22.svg
standings True
league_id 24
name Ligue 2
country France
season 2017
season_start 2017-07-28
season_end 2018-05-11
logo https://www.api-football.com/public/leagues/24.png
standings True
league_id 157
name National
country France
season 2017
season_start 2017-08-04
season_end 2018-05-11
logo https://www.api-football.com/public/leagues/157.png
standings True
league_id 206
name Feminine Division 1
country France
season 2017
season_start 2017-09-03
season_end 2018-05-27
logo https://www.api-football.com/public/leagues/206.png
standings True
CodeCupboard
  • 1,507
  • 3
  • 17
  • 26
  • I tried this approach but in my original data in dictionary nested_dictionary so many data that i receive the following error RecursionError: maximum recursion depth exceeded while calling a Python object –  Feb 25 '19 at 15:34
  • You can increase the recusursion depth if required 'import sys sys.setrecursionlimit(10000000000)' - you will need to choose a value to set this to. – CodeCupboard Feb 25 '19 at 15:37
  • see https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it – CodeCupboard Feb 25 '19 at 15:44