Iterate

  • break iterable into lists of length n: list(more_itertools.chunked(iterable, n)) - see
  • flatten a list of lists: list(itertools.chain.from_iterable(list_of_lists))

Dict

Iterate keys of dict

d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
    print(key, 'corresponds to', d[key])

Iterate keys and values of dict:

d = {'x': 1, 'y': 2, 'z': 3}
for key, value in d.items():
    print(key, 'corresponds to', value)
Last modified November 4, 2022: add flatten list of lists (cf0c368)