TRiBByX
2/26/2018 - 9:08 PM

iterate and is iterable script

A script that iterates over all items in a list or dicts with nested lists and/or dicts

from collections import Iterable

def is_iterable(data):
  if isinstance(value, Iterable):
    return True
  else:
    return False

def iterate(data, indent=''):
  if isinstance(data, dict):
    for key,value in data.items():
      if is_iterable(value):
        print indent + key
        iterate(value, indent+'  ')
      else:
        print key, value
  elif isinstance(data, list):
    for value in data:
      if is_iterable(value):
        iterate(value)
      else:
        print value