Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper use of tuples in Python

Tags:

python

Consider I have the following block of code 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 don't want to change the values in the lists. Because tuples are easier on memory and processor, should I be doing 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 homogenous? If tuples are better, can I go overboard and do this:

sets = tuple(sets)
like image 318
raul Avatar asked Mar 15 '23 11:03

raul


2 Answers

The difference between tuples and lists is meaning of order. Both tuples and lists are ordered sequences, but lists should be homogeneous while tuples often draw meaning from their order in sequence. An ordered pair, for example, is a tuple because

(3, 5)

Is a different object entirely than

(5, 3)

It appears in your case that have homogeneous data that doesn't need to be immutable and doesn't draw any meaning from its position. Therefore I would use a list of lists, not a list of tuples.

like image 197
Adam Smith Avatar answered Mar 31 '23 07:03

Adam Smith


It might use less memory in the end but the lists still get created temporarily and you do extra work converting them to tuples, so it's probably not easier on the processor.

Really you should be profiling your overall program, looking for the biggest performance weaknesses, and if you want to know if something will help, measure both possibilities.

like image 42
Alex Hall Avatar answered Mar 31 '23 06:03

Alex Hall