flati

Flatten nested iterable object (Pure-Python)


Keywords
flatten, generator, pure-python
License
MIT
Install
pip install flati==0.1.1

Documentation

flati

travis-ci.org coveralls.io pyversion latest version license

Flatten nested iterable object (Pure-Python implementation)

Installation

$ pip install flati

Usage

import flati

iterable = [(1, 2, 3), (4, (5, 6))]
list(flati.flatten(iterable))
# => [1, 2, 3, 4, 5, 6]

# flati.flatten() returns a generator
import types
isinstance(flati.flatten(iterable), types.GeneratorType)
# => True

# If you want to avoid flattening specific type, then use "ignore" parameter
iterable = [('abc'), ('def', ('g', 'hi'))]
list(flati.flatten(iterable, ignore=str))
# => ['abc', 'def', 'g', 'hi']

Tips

If you want to flatten numpy.ndarray, I recommend using following methods:

  • numpy.ravel()
  • ndarray.reshape(-1)
  • ndarray.flatten() # This method is a bit slow because it makes a copy