Skip to content

Instantly share code, notes, and snippets.

@goldhand
Created September 10, 2013 19:43
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 goldhand/6514540 to your computer and use it in GitHub Desktop.
Save goldhand/6514540 to your computer and use it in GitHub Desktop.
Pass a list of dict conditions to find a particular dict in a recursive set of dicts
def check_conditions(conditions={}, D={}):
for key in conditions:
if D[key] == conditions[key]:
continue
else:
return False
return True
def get_dict_from_list(DL=[], conditions=[], i=0, children_key='children'):
"""
looks through a list of dictionaries and returns dict, where dict[key] == value
:param args:
:param DL:
:param i:
"""
for D in DL:
if check_conditions(conditions=conditions[i], D=D):
i += 1
if len(conditions) > i:
if D[children_key]:
get_dict_from_list(DL=D[children_key], conditions=conditions, i=i, children_key=children_key)
else:
i -= 1
pass
else:
return D
@goldhand
Copy link
Author

Keep getting this error:

      41 
      42 def check_conditions(conditions={}, D={}):
---> 43     for key in conditions:
      44         if D[key] == conditions[key]:
      45             continue

      ValueError: too many values to unpack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment