Lists for homogeneous data, and tuples for heterogeneous data ... why?

It seems to me that this must have been asked earlier (maybe more than once), so there are potential apologies in advance, but I can not find it anywhere (here or through Google).

Anyway, when explaining the difference between lists and tuples in Python, the second thing mentioned after tuples are immutable is that lists are best for uniform data, and tuples are best for heterogeneous data. But no one seems to be thinking of explaining why this is the case. So why is this the case?

+6
source share
4 answers

The philosophical difference is most.

A tuple is designed to reduce fixed and given data values. For instance:

person = ("John", "Doe") 

So this example is a person who has a first and last name. The fixed nature of this is a critical factor. Not a data type. Both "John" and "Dow" are lines, but this is not the main thing. The advantage of this is a constant character:

  • You are never surprised to find the missing value. man always has two meanings. Always.

  • You are never surprised if you find something added. Unlike a dictionary, another bit of code cannot “add a new key” or attribute

This predictability is called unchanged. It's just a fantastic way of saying that it has a fixed structure.

One of the immediate advantages is that it can be used as a dictionary key. So:

 some_dict = {person: "blah blah"} 

works. But:

 da_list = ["Larry", "Smith"] some_dict = {da_list: "blah blah"} 

does not work.

Do not let the link to the element be similar (person [0] vs da_list [0]) discards you. person [0] is the name. da_list [0] is just the first item in the list at a given time.

+4
source

First of all, this guide is just right. You can use tuples for homogeneous data and lists for heterogeneous data, and there may be times when this is great. One important case is if you need a hash collection so that you can use it as a dictionary key; in this case you should use a tuple, even if all the elements are uniform in nature.

Also note that the homogeneous / heterogeneous distinction really applies to the semantics of the data, not just the types. The sequence of name, profession and address is likely to be considered heterogeneous, although all three can be represented as strings. Therefore, it is more important to think about what you are going to do with the data (i.e., will you really treat the elements the same way) than what they are.

However, I think one list of reasons is preferable for homogeneous data because it is mutable. If you have a list of several things of the same type, it may make sense to add another to the list or remove it; when you do this, you still have a list of things of the same type.

In contrast, if you have a collection of dissimilar varieties, this is usually because you have a fixed structure or “scheme” for them (for example, the first is an identification number, the second is a name, the third is an address or something yet). In this case, it makes no sense to add or remove an element from the collection, because the collection is an integrated whole with the specified roles for each element. You cannot add an element without changing the entire layout for what the elements represent.

In short, dimensional changes are more natural for homogeneous collections than for heterogeneous collections, so volatile types are more natural for homogeneous collections.

+13
source

This is not a rule, it is just a tradition.

In many languages, lists must be uniform, and tuples must be fixed. This applies to C ++, C #, Haskell, Rust, etc. Tuples are used as anonymous structures. Similarly, in mathematics.

A system such as Python, however, does not allow you to make these differences: you can create tuples of dynamic length, and you can create lists with heterogeneous data. That way, you are allowed to do whatever you need with lists and tuples in Python, which can be surprising to other people reading your code. This is especially true if people reading your code have experience in mathematics or are more familiar with other languages.

+3
source

Lists are often used to iterate over them and perform the same operation for each item in the list. Many list operations are based on this. For this reason, it is best for each element to be of the same type so as not to get an exception because the element was wrong.

Tuples are more structured data; they are immutable, therefore, if you process them correctly, you will not encounter type errors. This is the data structure that you will use if you specifically want to combine several types (for example, on-the-fly struct ).

0
source

Source: https://habr.com/ru/post/972536/


All Articles