tuples or lists

Dave Benjamin ramen at lackingtalent.com
Fri Mar 19 23:20:36 CET 2004


In article <c37nmd$58r$00$2 at news.t-online.com>, Martin v. Löwis wrote:
> beliavsky at aol.com wrote:
>> Is "use tuples instead of lists, when possible" a good rule?
> 
> The party line is: "use lists for similar items, tuples for
> different items". If you come from a C++ background, tuples
> are mostly used what structs are used for, and lists are used
> in the std::vector context.

I might or might not agree with this depending on the context. In most
cases, I would use a dictionary in Python where I would use a struct in C++
unless the struct was small and obvious, because the naming of values aids
in comprehension. I typically use tuples for:

 - points
 - ranges
 - the % operator (by design)
 - ordered lists of key/value pairs (such as for a SELECT/OPTION list)
 - groups of function arguments

None of these usages necessarily involves different items, but the last
three often do. In general, I really don't like the look of the
('singleton',) tuple, so in functions expecting sequences, I tend to use
lists for consistency.

-- 
.:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.
:  please talk to your son or daughter about parametric polymorphism. :



More information about the Python-list mailing list