Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to create very large tuples in Python?

I have a quite large list (>1K elements) of objects of the same type in my Python program. The list is never modified - no elements are added, removed or changed. Are there any downside to putting the objects into a tuple instead of a list?

On the one hand, tuples are immutable so that matches my requirements. On the other hand, using such a large tuple just feels wrong. In my mind, tuples has always been for small collections. It's a double, a tripple, a quadruple... Not a two-thousand-and-fiftyseven-duple.

Is my fear of large tuples somehow justified? Is it bad for performance, unpythonic, or otherwise bad practice?

like image 392
Anders Avatar asked May 15 '17 20:05

Anders


People also ask

How big can a tuple be in Python?

If you mean what the maximum size of a tuple or list is, I assume that it is very large. Most likely you would run out of memory before hitting some limit. Someone else can add to this if they have specific knowledge of the indexing, possible 2.1 or 4.2 billion items in 32-bit and 8 or 16 sextillion in 64-bit.

Do tuples have worse performance than lists?

Internally, tuples are stored a little more efficiently than lists, and also tuples can be accessed slightly faster.

What is the maximum number of elements that tuple can store?

Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing 'comma' to make it a tuple.

Are tuples more efficient than lists?

In contrary, as tuples are immutable and fixed size, Python allocates just the minimum memory block required for the data. As a result, tuples are more memory efficient than the lists.

What is the difference between lists and tuples in Python?

In CPython, go ahead. Under the covers, the only real difference between the storage of lists and tuples is that the C-level array holding the tuple elements is allocated in the tuple object, while a list object contains a pointer to a C-level array holding the list elements, which is allocated separately from the list object.

Why do we use giant tuples in Python?

In CPython, there can even be "a reason" to prefer giant tuples: the cyclic garbage collection scheme exempts a tuple from periodic scanning if it only contains immutable objects. Then the tuple can never be part of a cycle, so cyclic gc can ignore it.

How do you create a tuple in Python with no items?

To create a tuple in Python, add items with round brackets “ ()” like my_tuple = (“red”, “blue”, “green”) and to create an empty tuple, use empty round brackets ” () ” with no items in it. Table of Contents show

How to get the length of a tuple in Python?

After writing the above code (length of a tuple in python), Ones you will print “len (my_tuple)” then the output will appear as a “ 3 ”. Here, the len () method will give the length of an item in a tuple. You can refer to the below screenshot length of a tuple in python.


1 Answers

In CPython, go ahead. Under the covers, the only real difference between the storage of lists and tuples is that the C-level array holding the tuple elements is allocated in the tuple object, while a list object contains a pointer to a C-level array holding the list elements, which is allocated separately from the list object. The list implementation needs to do that because the list may grow, and so the memory containing the C-level vector may need to change its base address. A tuple can't change size, so the memory for it is allocated directly in the tuple object.

I've created tuples with millions of elements, and yet I lived to type about it ;-)

Obscure

In CPython, there can even be "a reason" to prefer giant tuples: the cyclic garbage collection scheme exempts a tuple from periodic scanning if it only contains immutable objects. Then the tuple can never be part of a cycle, so cyclic gc can ignore it. The same optimization cannot be used for lists; just because a list contains only immutable objects during one run of cyclic gc says nothing about whether that will still be the case during the next run.

This is almost never highly significant, but it can save a percent or so in a long-running program, and the benefit of exempting giant tuples grows the bigger they are.

like image 153
Tim Peters Avatar answered Oct 13 '22 21:10

Tim Peters