Skip to content

Instantly share code, notes, and snippets.

@wqlin
Created January 18, 2017 03:35
Show Gist options
  • Save wqlin/250d9dc82b4e35ccf6f1b3bee6edfdf7 to your computer and use it in GitHub Desktop.
Save wqlin/250d9dc82b4e35ccf6f1b3bee6edfdf7 to your computer and use it in GitHub Desktop.
flatten python's nested sequences
from collections import Iterable
def flatten(items, ignore_types=(str, bytes)):
"""flatten a nested sequence
implement it based on Python Cookbook(version 3) 4.14 Flattening a Nested Sequence"""
for x in items:
if isinstance(x, Iterable) and not isinstance(x, ignore_types):
yield from flatten(x, ignore_types)
else:
yield x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment