Skip to content

Instantly share code, notes, and snippets.

@Xpktro
Created March 19, 2015 19:46
Show Gist options
  • Save Xpktro/3ea02add27e323516ca6 to your computer and use it in GitHub Desktop.
Save Xpktro/3ea02add27e323516ca6 to your computer and use it in GitHub Desktop.
Get values from iterable consisting of dicts or lists in a flattened list
def values(iterable):
if type(iterable) == dict:
return sum([values(iterable.values())], [])
elif type(iterable) == list:
return sum(map(values, iterable), [])
return [iterable]
# >>> values([1, 2, 3, {'1': 2, '2': [4, 5, 6]}, [7, 8, 9]])
# [1, 2, 3, 2, 4, 5, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment