Created
December 9, 2019 07:53
-
-
Save msguner/781305356f0323f9cdc97cc537f1d3d3 to your computer and use it in GitHub Desktop.
Python get dictionary all nested keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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