Proper use of tuples in Python

I have the following code block in my program to read data from a large text file:

sets = []
for line in open(file, "r"):
    sets.append(line.split()) # sets is a list of lists

I do not want to change the values ​​in the lists. Since tuples are simpler in memory and processor, should I do the following instead?

sets = []
for line in open(file, "r"):
    sets.append(tuple(line.split())) # sets is a list of tuples

Or just use lists because the data is homogeneous? If tuples are better, I can go overboard and do this:

sets = tuple(sets)
+4
source share
2 answers

The difference between tuples and lists is the meaning of order. Both tuples and lists are ordered sequences, but lists must be uniform, while tuples often gain meaning from their order in the sequence. For example, an ordered pair is a tuple because

(3, 5)

This is a completely different object than

(5, 3)

, . , .

+5

, , , , , , .

, , , , -, .

+2

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


All Articles