Why are tuples in python needed? Foolproof? Of course, I understand that people are different, they can do all kinds of crap. But, if I create some object and mean that I am not going to change it, then I will not change it. Meaning to block enter a new object?
- Similar question in the English version of SO - MaxU
2 answers
Tuples are immutable objects and it has several advantages.
A tuple can be used, for example, to store information about connecting to a database and the speed of working with a tuple is faster than, for example, with a list. And it works faster because it is unchangeable.
A tuple also takes up less memory, which can be useful when it comes to large amounts of data.
Practical examples can be very diverse, I will give simple ones.
If you use expressions, then the expression will work faster with the tuple than with the list.
numbers = (1,2,3,4,5) squares_under_10 = (number*number for number in numbers if number*number < 10) In order to verify this, you can use timeit :
$ python -m timeit '(number*number for number in [1,2,3,4,5])' 1000000 loops, best of 3: 0.591 usec per loop $ python -m timeit '(number*number for number in (1,2,3,4,5))' 1000000 loops, best of 3: 0.472 usec per loop On small amounts of data, this may not be noticeable, however, if there are many iterable values, the benefit becomes noticeable.
* I will mark one moment under the asterisk.
If you put a list in a tuple, you can change it:
T = ([1,2,3], 4, 5, 6); T[0][0] = 10; print(T[0][0]) #выведет 10 Here you change the list inside the tuple, but not the tuple itself. However, you cannot delete the first (zero) element of the tuple or replace it with another data type.
This has a number of advantages when you need a specific data structure (immutable), with a given set of elements, but at the same time some objects inside the tuple can change during work (for example, a list).
- Could you measure the time of working with and without a tuple, as it is done in the English version? Then put the "correct answer" - hedgehogues
- @hedgehogues updated the answer - Mr. Brightside
- squares_under_10 is not a tuple, but a generator, and the speed of its execution may be slower than the list - vadim vaduxa
- @vadimvaduxa That's right, but the numbers are a tuple - Mr. Brightside
Translation of the answer from the English stack :
- Unchangeable objects allow significant optimization; This seems to be similar to unchanging strings in Java, developed entirely separately, but at about the same time as Python, and almost everything is constant in truly functional languages.
In Python, in particular, only immutable can be hashed (and, therefore, members of sets or keys in dictionaries). Again, this allows for serious optimization. Creating decent hash tables that store fully modifiable objects is a nightmare — either you take copies of everything you have, or you are tempted to check if the object hash has changed since the last link to it. An example of an optimization problem:
$ python -mtimeit '["fee", "fie", "fo", "fum"]' loops, best of 3: 0.432 usec per
$ python -mtimeit '("fee", "fie", "fo", "fum")' loops, best of 3: 0.0563 usec per loop
- A common agreement is worth mentioning: "Tuples are for heterogeneous data, list are for homogeneous data. Tuples are not read-only lists." - jfs