The trick to write flatten concisely in python

If you use python2, you can write smartly with sum!

I realized that I could write smartly if I thought that there was only a delicate way to do it.

Python supports addition between lists,

[1,2,3] + [4,5,6] => [1,2,3,4,5,6]

Utilizing the fact that the sum function can specify the initial value as the second argument,

data = [[1,2,3], [4,5,6], [7,8,9]]

sum(data, [])

You can write like this.

That said, it's a bit inflexible that tuples and generators aren't supported unless it's a list.

Supplement 1

In python3 series, sum seems to check the type, so this method cannot be used. Remorse.

Supplement 2

By using this, I wondered if it would be possible to process all instances that implement __add__ with sum.

>>> a = [1,2,3]
>>> data = [a, [4,5,6], [7,8,9]]
>>> sum(data,[])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a
[1, 2, 3]
>>> empty = []
>>> sum(data,empty)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> empty
[]
>>>

Internally, it is expected that the result of addition is used for processing.

So, if you pass a class with __add__ as shown below,

>>> class A:
...   def __add__(self, target):
...     self.target = target
...     return 100
...
>>> a = A()
>>> sum([5,10,15,20],a)
145
>>> a.target
5

... Well, I understand that you shouldn't make __add__ a destructive implementation.

Recommended Posts

The trick to write flatten concisely in python
I want to write in Python! (3) Utilize the mock
In the python command python points to python3.8
Flatten in python
flatten in python
Write the test in a python docstring
Various comments to write in the program
How to write Ruby to_s in Python
The 15th offline real-time how to write reference problem in Python
I wrote the code to write the code of Brainf * ck in python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
How to use the C library in Python
The 17th offline real-time how to write reference problem implemented in Python
How to write the correct shebang in Perl, Python and Ruby scripts
To dynamically replace the next method in python
Write Python in MySQL
Draw graphs in Julia ... Leave the graphs to Python
How to get the files in the [Python] folder
To write to Error Repoting in Python on GAE
I want to display the progress in Python!
I want to write in Python! (1) Code format check
20th Offline Real-time How to Write Problems in Python
How to retrieve the nth largest value in Python
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to know the current directory in Python in Blender
I want to write in Python! (2) Let's write a test
Convert the image in .zip to PDF with Python
Write letters in the card illustration with OpenCV python
Write tests in Python to profile and check coverage
Write data to KINTONE using the Python requests module
Write a log-scale histogram on the x-axis in python
How to use the model learned in Lobe in Python
[Python] How to output the list values in order
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
I want to use the R dataset in python
Python OpenCV tried to display the image in text.
Write Pandoc filters in Python
To flush stdout in Python
Download the file in Python
Write beta distribution in Python
Login to website in Python
Speech to speech in python [text to speech]
Write to csv with Python
How to develop in Python
Post to Slack in Python
[Note] How to write QR code and description in the same image with python
How to write custom validations in the Django REST Framework
[python] How to check if the Key exists in the dictionary
Output "Draw ferns programmatically" to the drawing process in Python
How to debug the Python standard library in Visual Studio
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
Write a python program to find the editing distance [python] [Levenshtein distance]
[Python] How to write an if statement in one sentence.
Various ways to calculate the similarity between data in python
How to get the last (last) value in a list in Python
To write a test in Go, first design the interface
I tried to implement the mail sending function in Python
I didn't want to write the AWS key in the program