Efficiency: list or tuple?

Terry Hancock hancock at anansispaceworks.com
Wed Jan 15 15:14:49 EST 2003


Spencer Ernest Doidge wrote:
> I'm interested in putting pairs of integers and floats into a sequence
> of indeterminate length.
> I'm collecting this data in real time, and I want to use the most
> efficient available method so as to be able to gather data as fast as
> possible and append it to the sequence as I go.
> As far as I can tell, I have lists and tuples to choose from.
> Does either of these structures work better in this situation, or is
> there some other way I don't know about?

You almost certainly want a list of tuples.

Tuples are useful for fixed-size representations of ordered data with 
minimum overhead, while lists are good for indeterminate length data, 
especially if it might have to be reorganized or altered in place.

In some cases, you might want a list of class instances rather than tuples. 
Class instances can have named data (and of course, methods) associated 
with them, which can be useful. But that's much more overhead than using a 
tuple, so it really depends on your application.

Cheers,
Terry

-- 
Anansi Spaceworks
http://www.anansispaceworks.com




More information about the Python-list mailing list