Let's stop copying. Introducing flati, a module to flatten with Python

Ciao ... †

Introduction

Sometimes you want to combine nested (nested) lists in Python into a single list, that is, flatten it. Information on how to deal with such cases can be found immediately by searching the Web. But isn't it annoying to copy and paste it one by one? I'm annoyed. So, in this article, I will introduce the module flati that flattens in Python.

Currently it supports Python 2.7 and Python 3.4 ~ 3.8. Since it is a Pure-Python module, it will work on various platforms such as Windows / macOS / Ubuntu as long as it has a compatible Python environment.

Origin of the name

I named it flati by applying flatten processing and Fulvio Frati (a teacher who has studied with me). Please pronounce "Furate".

Installation

$ pip install flati

or

$ python -m pip install flati

How to use


import flati

# flati.flatten()Is a generator that returns nested Iterable objects element by element.
#If you want to combine the returned values into one Iterable object, list()Please use etc.
iterable = [(1, 2, 3), (4, (5, 6))]
list(flati.flatten(iterable))
# => [1, 2, 3, 4, 5, 6]

#I will write it again. flati.flatten()Is a generator.
import types
isinstance(flati.flatten(iterable), types.GeneratorType)
# => True

#If you don't want to flatten a particular Iterable object,"ignore"Please specify with an argument.
iterable = [('abc'), ('def', ('g', 'hi'))]
list(flati.flatten(iterable, ignore=str))
# => ['abc', 'def', 'g', 'hi']

Tips If you want to flatten NumPy's ndarray, use the NumPy function. That is faster.

Reference: https://python.atelierkobato.com/flat/

Preceding case

I will introduce what I found by searching for "flatten" with PyPI. Those that flatten the dict type are excluded.

I'm tired so I'll leave it around here.

Benefits of using flati

--You don't have to copy the code that comes out by searching the Web --Any Iterable object is OK --ʻIgnore` You can specify the type you do not want to flatten with the argument --Since it is a generator, memory usage is easy when handling huge data. --The number of characters in the module name is small (5 characters) --Simple implementation (source code) --Pure-Python module, so feel free to run on various platforms --Issue and Pull request for request and bug report / fix can be in Japanese (title should be in English) --Click here for details: https://github.com/ikegami-yukino/flati/blob/master/CONTRIBUTING.md

A big request

――If you like it, please give a star to flati's GitHub repository. With just one click, my development motivation is up. ――Stars are not enough! If you like, you can send me money or things by pressing the Sponsor button on flati's GitHub repository. The motivation for my development will increase considerably.

at the end

For the next few days, I would like to introduce modules that I made for the time being but did not advertise at all.

Shiver and wait ... †

Recommended Posts

Let's stop copying. Introducing flati, a module to flatten with Python
Let's make a GUI with python.
Let's make a graph with python! !!
Let's feel like a material researcher with python [Introduction to pymatgen]
Let's make a shiritori game with Python
[Introduction to Python] Let's use foreach with Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
[Python] How to deal with module errors
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Error when installing a module with Python pip
Try to draw a life curve with python
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
How to add a Python module search path
Decide to assign a laboratory with Python (fiction)
Steps to create a Twitter bot with python
To add a module to python put in Julialang
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Let's make a module for Python using SWIG
I want to write to a file with Python
A layman wants to get started with Python
If you want to make a discord bot with python, let's use a framework
How to convert / restore a string with [] in python
A memo connected to HiveServer2 of EMR with python
Try to make a Python module in C language
Try to make a command standby tool with python
Python Ver. To introduce WebPay with a little code.
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
I made a Python module to translate comment outs
I want to work with a robot in python.
From buying a computer to running a program with python
I tried to automatically generate a password with Python3
[Python] A memo to write CSV vertically with Pandas
Introducing Python 2.7 to CentOS 6.6
A program to write Lattice Hinge with Rhinoceros with Python
A memo of misunderstanding when trying to load the entire self-made module with Python3
[Python] How to create a 2D histogram with Matplotlib
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
[Super easy] Let's make a LINE BOT with Python.
I want to run a quantum computer with Python
[Python] How to draw a scatter plot with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
Let's create a program that automatically registers ID/PW from CSV to Bitwarden with Python + Selenium
Let's make a websocket client with Python. (Access token authentication)
How to install NPI + send a message to line with python
How to convert an array to a dictionary with Python [Application]
Create a Mastodon bot with a function to automatically reply with Python
I made a package to filter time series with python
I wrote a program quickly to study DI with Python ①
A collection of competitive pro techniques to solve with Python
Let's create a script that registers with Ideone.com in Python.
Probably the easiest way to create a pdf with Python3
Experiment to make a self-catering PDF for Kindle with Python
Let's create a PRML diagram with Python, Numpy and matplotlib.
[python] A note when trying to use numpy with Cython