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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With