Each tuple represents one key/value pair from your dictionary. : items.extend(flatten(v, new_key, sep=sep). Here is how to turn each dictionary into a set we can use: set1 set (ems ()) set2 set (ems ()) This returns a set containing a series of tuples. If I understand the problem you are trying to solve it is to the compare keys of the two dictionaries (as well as subkeys of nested. : if isinstance(v, collections.MutableMapping): Any easy fix is to change this line: if isinstance (v, dict): klist.extend (getkeys (v)) to: if isinstance (v, dict): klist.append (getkeys (v)) Same idea, but the edited version preserves nested key levels. : new_key = parent_key + sep + k if parent_key else k You should use a variable name that isnt the. You wont be able to do this in your snippet because youve redefined the name list to your dictionary. If you want to compare the contents, convert them to lists, using the list () function. : def flatten(d, parent_key='', sep='_'): keys () and values () dont return lists, they return iterator objects that yield the keys and values. ![]() I am trying to recursively compare below two python dictionaries: expectededr = įor flattening your dictionaries, we can use the approach suggested in Flatten nested Python dictionaries, compressing keys: In : import collections If you want to check each key and value you can use ems and dict.get with a default value: for k,v in ems (): if serverdict.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |