Python: I tried to make a flat / flat_map just right with a generator

I couldn't find anything just right, so I made it myself. Is it somewhere? What is "just right":


flat = lambda xs : (
    y
    for x in xs
    for y in (
        [x] if type(x) is not list else
        x
        )
    )

Those who are not on the list are the strategy to lift them up on the list and then disassemble them. I made it a generator according to the style of Python.

With this, flat_map looks like this:

flat_map = lambda f, xs : flat( map( f, xs ) )

If you have any other suggestions, please leave a comment.

Recommended Posts

Python: I tried to make a flat / flat_map just right with a generator
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I want to make a game with Python
I tried to make a generator that generates a C # container class from CSV with Python
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to draw a route map with Python
I tried to automatically generate a password with Python3
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a simple mail sending application with tkinter of Python
[Patent analysis] I tried to make a patent map with Python without spending money
[Python] When I tried to make a decompression tool with a zip file I just knew, I was addicted to sys.exit ()
I tried to make various "dummy data" with Python faker
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to make a real-time sound source separation mock with Python machine learning
I tried a functional language with Python
I tried to make a Web API
Rubyist tried to make a simple API with Python + bottle + MySQL
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to make a strange quote for Jojo with LSTM
I tried to make an image similarity function with Python + OpenCV
I tried to make a mechanism of exclusive control with Go
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
I also tried to imitate the function monad and State monad with a generator in Python
I tried to communicate with a remote server by Socket communication with Python.
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to make a calculator with Tkinter so I will write it
I tried to get CloudWatch data with Python
Try to make a "cryptanalysis" cipher with Python
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
[AWS] [GCP] I tried to make cloud services easy to use with Python
I tried to output LLVM IR with Python
I tried to discriminate a 6-digit number with a number discrimination application made with python
I tried to automate sushi making with python
Try to make a dihedral group with Python
[Outlook] I tried to automatically create a daily report email with Python
I tried to build a Mac Python development environment with pythonz + direnv
I want to write to a file with Python
[Zaif] I tried to make it easy to trade virtual currencies with Python
I tried to make a url shortening service serverless with AWS CDK
I tried to make a ○ ✕ game using TensorFlow
When I tried to make a VPC with AWS CDK but couldn't make it
I tried to explain what a Python generator is for as easily as possible.
When I tried to create a virtual environment with Python, it didn't work
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Machine learning beginners tried to make a horse racing prediction model with python
I tried to easily create a fully automatic attendance system with Selenium + Python