11
{'action_name':'mobile signup',
    'functions':[{'name':'test_signUp',
                  'parameters':{'username':'max@getappcard.com',
                                'password':'12345',
                                'mobileLater':'123454231',
                                'mobile':'1e2w1e2w',
                                'card':'1232313',
                                'cardLater':'1234321234321'}}],
    'validations':[
            {'MOB_header':'My stores'},
            {'url':"/stores/my"}]}

I want to get all the keys & values of this dict as a list (out of values that they are dict or array)

print result should be like this:

action name = mobile signup
name = test_signUp
username : max@getappcard.com
password : 12345
mobileLater: 123454231
mobile : 1e2w1e2w
card : 1232313 
cardLater : 1234321234321
MOB_header : My stores
MERose
  • 4,048
  • 7
  • 53
  • 79
eligro
  • 785
  • 3
  • 13
  • 23
  • Maybe [this](http://stackoverflow.com/questions/1679384/converting-python-dictionary-to-list) will help? – Hindol May 13 '12 at 06:05
  • And what have you tried? Show us some code. – Vikas May 13 '12 at 06:06
  • For full generality, you should use `else:` instead of `elif isinstance(value, str):`. – huon May 13 '12 at 06:43
  • 2
    there is one problem here: if you have nested entries with the same key, they will overwrite each other. hopefully that's nto a problem here, but you should be aware. – Jeff Tratner May 13 '12 at 17:30
  • known issue. my purpose is not to add to dict, but only to run function on each of the key+value and then update the dictIn itself. – eligro May 14 '12 at 02:18

3 Answers3

9

You might want to use a recursive function to extract all the key, value pairs.

def extract(dict_in, dict_out):
    for key, value in dict_in.iteritems():
        if isinstance(value, dict): # If value itself is dictionary
            extract(value, dict_out)
        elif isinstance(value, unicode):
            # Write to dict_out
            dict_out[key] = value
    return dict_out

Something of this sort. I come from C++ background so I had to google for all the syntaxes.

Jamie Bull
  • 12,889
  • 15
  • 77
  • 116
Hindol
  • 2,924
  • 2
  • 28
  • 41
3

I have modified a little bit from this link to get all keys&values in nested dict of list-of-dicts and dicts:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield (key, value)
            yield from recursive_items(value)
        elif type(value) is list:
            yield (key, value)
            for i in value:
                if type(i) is dict:
                    yield from recursive_items(i)
        else:
            yield (key, value)

for i in recursive_items(your_dict):
    print(i) #print out tuple of (key, value)

Output:

('action_name', 'mobile signup')
('functions', [{'name': 'test_signUp', 'parameters': {'username': 
'max@getappcard.com', 'password': '12345', 'mobileLater': '123454231', 'mobile': 
'1e2w1e2w', 'card': '1232313', 'cardLater': '1234321234321'}}])
('name', 'test_signUp')
('parameters', {'username': 'max@getappcard.com', 'password': '12345', 
'mobileLater': '123454231', 'mobile': '1e2w1e2w', 'card': '1232313', 
'cardLater': '1234321234321'})
('username', 'max@getappcard.com')
('password', '12345')
('mobileLater', '123454231')
('mobile', '1e2w1e2w')
('card', '1232313')
('cardLater', '1234321234321')
('validations', [{'MOB_header': 'My stores'}, {'url': '/stores/my'}])
('MOB_header', 'My stores')
('url', '/stores/my')
0

a little late but for python 3.8 you can use yield from

def dictitems2list(d):
    for k, v in d.items():
        yield k
        if isinstance(v, dict):
            yield from get_all_items(v)
        else:
            yield v

all_itemt = list(dict2items(d))
tom
  • 31
  • 4