1

How can I find the certain key in a dictionary, with dictionaries, and I don't know exactly how many dicts I have in a dict and depth of each dict. I need pass through this dict and find key - 'status'

I have this error:

RecursionError: maximum recursion depth exceeded in comparison

d = {'value': '1', 'unrestricted_value': '1', 'data': {'kpp': '550701001', 'capital': None, 'management': {'name': '3', 'post': 'd', 'disqualified': None}, 'founders': None, 'managers': None, 'predecessors': None, 'successors': None, 'branch_type': 'MAIN', 'branch_count': 0, 'source': None, 'qc': None, 'hid': '8e9c98d8d72c43551ae86fd61bc21dc088b81863b2b24ecb4a657d1c4ab72593', 'type': 'LEGAL', 'state': {'status': 'ACTIVE', 'code': None, 'actuality_date': 1560470400000, 'registration_date': 1243468800000, 'liquidation_date': None}, 'opf': {'type': '2014', 'code': '12300', 'full': 'j', 'short': 'ООО'}, 'name': {'full_with_opf': 'sbstbb', 'short_with_opf': 'y', 'latin': None, 'full': 'y', 'short': 'r'}, 'inn': '5505', 'ogrn': '109', 'okpo': '6136', 'okato': '52401364000', 'oktmo': '5270', 'okogu': '0014', 'okfs': '16', 'okved': '43.21', 'okveds': None, 'authorities': None, 'documents': None, 'licenses': None, 'finance': {'tax_system': None, 'income': None, 'expense': None, 'debt': None, 'penalty': None, 'year': None}, 'address': {'value': '5', 'unrestricted_value': '6', 'data': {'postal_code': '3', 'country': 't', 'country_iso_code': 'yu', 'federal_district': '3', 'region_fias_id': 'sdfg', 'region_kladr_id': '55000000', 'region_iso_code': 'nrn', 'region_with_type': 'Омская обл', 'region_type': 'обл', 'region_type_full': 'область', 'region': 'Омская', 'area_fias_id': None, 'area_kladr_id': None, 'area_with_type': None, 'area_type': None, 'area_type_full': None, 'area': None, 'city_fias_id': '27bf-4519-9ea0-6185d681d44e', 'city_kladr_id': '550000', 'city_with_type': 'г Омск', 'city_type': 'г', 'city_type_full': 'y', 'city': 'j', 'city_area': None, 'city_district_fias_id': None, 'city_district_kladr_id': None, 'city_district_with_type': 'g', 'city_district_type': 'округ', 'city_district_type_full': 'nh', 'city_district': 'b', 'settlement_fias_id': None, 'settlement_kladr_id': None, 'settlement_with_type': None, 'settlement_type': None, 'settlement_type_full': None, 'settlement': None, 'street_fias_id': '5043c277-0073-4000-b319', 'street_kladr_id': '55000001000005900', 'street_with_type': 'dsbesb', 'street_type': 'zvdf', 'street_type_full': 'бульвар', 'street': 'a', 'stead_fias_id': None, 'stead_cadnum': None, 'stead_type': None, 'stead_type_full': None, 'stead': None, 'house_fias_id': '39ab7e11-3980-46ba-816b-8f3e36dfad10', 'house_kladr_id': '5500000100000590021', 'house_cadnum': None, 'house_type': 'д', 'house_type_full': 'v', 'house': '3', 'block_type': 'к', 'block_type_full': 'корпус', 'block': '8', 'entrance': None, 'floor': None, 'flat_fias_id': None, 'flat_cadnum': None, 'flat_type': 'кв', 'flat_type_full': 'zdfbn', 'flat': '172/173', 'flat_area': None, 'square_meter_price': '54234', 'flat_price': None, 'postal_box': None, 'fias_id': '39ab7e11-3980-46ba-816b-8f3e36dfad10', 'fias_code': '55000001000000000590021', 'fias_level': '8', 'fias_actuality_state': '0', 'kladr_id': '5500000100000590021', 'geoname_id': '1496153', 'capital_marker': '2', 'okato': '52401364000', 'oktmo': '52701000001', 'tax_office': '5507', 'tax_office_legal': '5507', 'timezone': 'UTC+6', 'geo_lat': '54.991044', 'geo_lon': '73.3139787', 'beltway_hit': None, 'beltway_distance': None, 'metro': None, 'divisions': None, 'qc_geo': '1', 'qc_complete': None, 'qc_house': None, 'history_values': None, 'unparsed_parts': None, 'source': 'fg', 'qc': '0'}}, 'phones': None, 'emails': None, 'ogrn_date': 1243468800000, 'okved_type': '2014', 'employee_count': None}}

key = 'status'

def find_key(d, key):
    for i in dict:
        if i == key:
            print(i[key])
        elif i != key:
            return find_key(i, key)
print(find_key(d, 'status'))

Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
  • 1
    Do you know what `i` is in the loop? Based on that, do you think passing it in as a dict will be helpful? – Mad Physicist Jul 20 '22 at 19:09
  • What is `j_str`? – Mad Physicist Jul 20 '22 at 19:09
  • 1
    Don't use `dict` as a variable name. – Mad Physicist Jul 20 '22 at 19:09
  • The input argument is `d`, but you aren't actually using that variable anywhere in the function... – John Gordon Jul 20 '22 at 19:27
  • Does this answer your question? [Find all occurrences of a key in nested dictionaries and lists](https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists) – Alireza75 Jul 20 '22 at 19:30
  • The last edit was not an improvement... – Mad Physicist Jul 20 '22 at 19:33
  • Also, if you answer the first question I asked you, I think you will find the answer to your question. – Mad Physicist Jul 20 '22 at 19:34
  • 1
    If this is a JSON you get from (for example) a web request, then you KNOW the value is at `d['data']['state']['status']`. No need to search. – Tim Roberts Jul 20 '22 at 20:29
  • I agree, what ya doin is kind of dangerous. it's like blowing up a mountain with a stack of dynamite to get at the hidden mine underneath it. I mean not necessarily bad or anything, but I mean there's probably a swiss army knife approach that might be better suited to this task, as @TR mentions above. – rv.kvetch Jul 20 '22 at 21:00
  • d['data']['state']['status'] - i did this already myself)) it`s easiest way, but what if the another response would be with another amount of dicts and nesting – Sergey Leonov Jul 21 '22 at 05:56

1 Answers1

3

Здравствуй, человек) Did not completely understand your question, but here is what I can offer:

key = "status"
def find_key(d, key):
    for d_key in d.keys():
        if d_key == key:
            print(d[key])
        else:
            if isinstance(d[d_key], dict):
                 find_key(d[d_key], key)
find_key(d, key)
Beholder
  • 111
  • 8