Skip to content

Instantly share code, notes, and snippets.

@msguner
Created December 9, 2019 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msguner/781305356f0323f9cdc97cc537f1d3d3 to your computer and use it in GitHub Desktop.
Save msguner/781305356f0323f9cdc97cc537f1d3d3 to your computer and use it in GitHub Desktop.
Python get dictionary all nested keys
def get_keys(dl, key_list):
if isinstance(dl, dict):
for k, v in dl.items():
key_list.append(k)
if isinstance(v, list) or isinstance(v, dict):
get_keys(v, key_list)
if isinstance(dl, list):
for i in dl:
if isinstance(i, list) or isinstance(i, dict):
get_keys(i, key_list)
dic = {'k1': 1, 'k2': 2, 'k3': [{'k31': 31, 'k2': 32, 'k33': 33}]}
keys_list = []
get_keys(dic, keys_list)
print(keys_list)
#[k1,k2,k3,k31,k32,k33]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment