List vs tuples

Roy Smith roy at panix.com
Thu Apr 8 17:20:50 EDT 2004


"John Roth" <newsgroups at jhrothjr.com> wrote:
> A list is a data structure that is intended to be used with 
> homogenous members; that is, with members of the same type.

There's really nothing about a list which makes it more suitable for 
homogeneous elements than a tuple.  It's true that people do tend to 
think tuple when they want a mixed collection of objects, but there's 
nothing in the language semantics which makes that so.

I've written lots of code which parses data files.  If you've got a 
sequence of arbitrary type objects, the easiest way to process them is 
often to read them sequentially and append them to a list.  I don't see 
anything wrong with that.  For example, I once had to write some code 
which parsed SQL statements that looked something like:

insert into foo values (1, 2, 'foo', 3.14, 'bar', 4, 5, 'baz', ...);

There were a total of about 30 items of mixed type (strings, integers, 
and floats) in the ()'s. 

One of the really nice things about Python (vs. C++ or Java) is that 
you're not bound by the tyranny of static typing.  This is a perfect 
example of how this makes life so simple.



More information about the Python-list mailing list