[Python] I want to make a nested list a tuple

It is a common requirement to make a list a tuple and vice versa.

General method

A common practice is to use the built-in functions list and tuple.

l = [0, 1, 2]
t = tuple(l)
print(t)
(0, 1, 2)

The problem with this method

However, if the list is nested, it will only tuple the shallowest hierarchy.

l = [[0, 1, 2], [3, 4, 5, 6, 7], 8, 9]
t = tuple(l)
print(t) # ((0, 1, 2), (3, 4, 5, 6, 7), 8, 9)I want you to be
([0, 1, 2], [3, 4, 5, 6, 7], 8, 9)

It's sharp.

A function that tuples a nested list internally

Let's make a tuple of all layers while calling recursively.

def list_to_tuple(l):
    return tuple(list_to_tuple(e) if isinstance(e, list) else e for e in l)
l = [[0, 1, 2], [3, 4, 5, 6, 7], 8, 9]
t = list_to_tuple(l)
print(t)
((0, 1, 2), (3, 4, 5, 6, 7), 8, 9)

I did it.

Original material and its comparison

Actually, this article is [here](https://blog.ttk1.net/2016/05/20/python%E3%81%A7%E5%85%A5%E3%82%8C%E5%AD%90% E6% A7% 8B% E9% 80% A0% E3% 81% AElist% E3% 82% 92tuple% E3% 81% AB% E5% A4% 89% E6% 8F% 9B% E3% 81% 97% E3% 81% 9F% E3% 81% 84 /) It is a pakuri. ~~ Are you not embarrassed ~~

The difference is that the part that used the for statement and the assignment operator over there is now included.

For for statements that repeat honestly, the inclusion notation calls a dedicated process, which shortens the execution time (Reference).

def list_to_tuple_orig(_list):
    t = ()
    for e in _list:
        if isinstance(e,list):
            t += (list_to_tuple(e),)
        else:
            t += (e,)
    return t
l = list(range(10000))
%timeit t = list_to_tuple_orig(l)
%timeit t = list_to_tuple(l)
%timeit t = tuple(l)
92.7 ms ± 576 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
877 µs ± 3.31 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
19.6 µs ± 47.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

It's about 100 times faster. It's a big defeat for built-in functions.

Recommended Posts

[Python] I want to make a nested list a tuple
I want to make a game with Python
I want to make input () a nice complement in python
I want to build a Python environment
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to make a parameter list from CloudFormation code (yaml)
I want to make matplotlib a dark theme
I want to create a window in Python
I want to merge nested dicts in Python
I want to make C ++ code from Python code!
I want to write to a file with Python
I want to make a web application using React and Python flask
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to run a quantum computer with Python
python / Make a dict from a list.
I want to debug with Python
[Python] How to make a list of character strings character by character
I want to make a blog editor with django admin
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to make a click macro with pyautogui (desire)
I want to make a click macro with pyautogui (outlook)
I want to send a message from Python to LINE Bot
I want to make the Dictionary type in the List unique
I tried to make a stopwatch using tkinter in python
[Python] I want to use only index when looping a list with a for statement
[5th] I tried to make a certain authenticator-like tool with python
I want to use a wildcard that I want to shell with Python remove
I want to print in a comprehension
[2nd] I tried to make a certain authenticator-like tool with python
[Python] How to convert a 2D list to a 1D list
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
[3rd] I tried to make a certain authenticator-like tool with python
I want to use jar from python
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
I want to do a full text search with elasticsearch + python
I want to analyze logs with Python
python> Convert tuple to list> aList = list (pi_tuple)
I want to play with aws 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 want to make an automation program!
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
I tried to make a Web API
[1st] I tried to make a certain authenticator-like tool with python
When I got a list of study sessions in Python, I found something I wanted to make
I want to make a voice changer using Python and SPTK with reference to a famous site
[Introduction to Python] What is the difference between a list and a tuple?
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
I want to convert a table converted to PDF in Python back to CSV
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)