0

I have dict

{
    "count_casinos": 4,
    "casinos": {
        "16": {
            "fields": [
                {"casino_logo": "assets/images/crown.png"},
                {"raiting_casino": "9"},
                {"bonus_code": "AX7772"},
                {"bonus_summa": "200"},
                {"bonus_min_depozit": "1000000"},
                {"bonus_veizdjer": "4"}],
            "title": "Royal casino casino1"},
        "17": {
            "fields": [
                {"casino_logo": "assets/images/crown.png"},
                {"raiting_casino": "8.2"},
                {"bonus_code": "AX777"},
                {"bonus_summa": "200"},
                {"bonus_min_depozit": "1000000"},
                {"bonus_veizdjer": "4"}],
            "title": "Royal casino casino3"},
        "18": {
            "fields": [
                {"casino_logo": "assets/images/crown.png"},
                {"raiting_casino": "9.3"},
                {"bonus_code": "AX7772"},
                {"bonus_summa": "200"},
                {"bonus_min_depozit": "1000000"},
                {"bonus_veizdjer": "4"}],
            "title": "Royal casino casino2"},
        "15": {
            "fields": [
                {"casino_logo": "assets/images/crown.png"},
                {"raiting_casino": "9.9"},
                {"bonus_code": "AX777"},
                {"bonus_summa": "200"},
                {"bonus_min_depozit": "1000000"},
                {"bonus_veizdjer": "4"}],
            "title": "Royal casino casino4"}}}

when I try loop

     for k,v in casino.iteritems():
         for iterm in v['fields']:
             print iterm['bonus_code']

key:error

what's wrong?

Aaron
  • 10,133
  • 1
  • 24
  • 40
  • Is `casino` a dict with provided data? – vishes_shell Oct 03 '16 at 15:14
  • @Aaron the issue is not that the key does not exist. OP just isn't getting far enough into the nested dictionary to access it. – roganjosh Oct 03 '16 at 15:20
  • You are not going deep enough, `for k, v in casino.iteritems():` will only iterate once, for the `16` key. – Efferalgan Oct 03 '16 at 15:20
  • the very first item: ` "count_casinos": 4` will have no `['fields']` – Aaron Oct 03 '16 at 15:21
  • also, the second loop will query each dict in the list separately, and only one of them has a `'bonus_code'` field, so all the others will generate a key index error – Aaron Oct 03 '16 at 15:23

2 Answers2

1

If you iterate through v['fields'] each item will be dict object with only one key, and only one item have 'bonus_code' key.

Slight change that won't get you an error

 for k,v in casino.iteritems():
     for iterm in v['fields']:
         if 'bonus_code' in iterm: print iterm['bonus_code']

NOTE: if casino is initial_data dict .get('casinos')

UPDATE

I made a guess that OP's casino variable is

data = {"count_casinos": 4, "casinos": {"16": {"fields": [{"casino_logo": "assets/images/crown.png"}, {"raiting_casino": "9"}, {"bonus_code": "AX7772"}, {"bonus_summa": "200"}, {"bonus_min_depozit": "1000000"}, {"bonus_veizdjer": "4"}], "title": "Royal casino casino1"}, "17": {"fields": [{"casino_logo": "assets/images/crown.png"}, {"raiting_casino": "8.2"}, {"bonus_code": "AX777"}, {"bonus_summa": "200"}, {"bonus_min_depozit": "1000000"}, {"bonus_veizdjer": "4"}], "title": "Royal casino casino3"}, "18": {"fields": [{"casino_logo": "assets/images/crown.png"}, {"raiting_casino": "9.3"}, {"bonus_code": "AX7772"}, {"bonus_summa": "200"}, {"bonus_min_depozit": "1000000"}, {"bonus_veizdjer": "4"}], "title": "Royal casino casino2"}, "15": {"fields": [{"casino_logo": "assets/images/crown.png"}, {"raiting_casino": "9.9"}, {"bonus_code": "AX777"}, {"bonus_summa": "200"}, {"bonus_min_depozit": "1000000"}, {"bonus_veizdjer": "4"}], "title": "Royal casino casino4"}}}
casino = data.get('casinos')

because if casino would be initial dict that OP presented, then OP will get another error(TypeError), because values of data['count_casinos'] is int and int don't have __getitem__ method which would raise TypeError.

vishes_shell
  • 22,409
  • 6
  • 71
  • 81
0

You weren't going deep enough into the nested dictionaries. If you are only interested in the casinos section of data, something like this would work for you.

for k,v in casino['casinos'].iteritems():
    data_list = v.get('fields')

    # If you know the list index is consistent
    print data_list[2].get('bonus_code')

    #otherwise
    for item in data_list:
        if 'bonus_code' in item.keys():
            print item['bonus_code']
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • and what about others keys? – Vladleelee Lee Oct 03 '16 at 15:32
  • @VladleeleeLee I don't know what you're asking me. My code completes the task in your question; if you want to do something similar for other keys then you just need to make alterations to the structure of my answer to adapt it to your needs. – roganjosh Oct 03 '16 at 15:35