469,328 Members | 1,253 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

tuples or lists

Tuples, unlike lists, are immutable, which I crudely translate to mean
"their contents cannot be changed". Out of habit, I use only lists,
not tuples, in my code. But I wonder if this is poor style -- putting
a collection of data in a tuple, when possible, makes it easier to
follow the code. You do not have to think about whether contents have
changed from where the tuple was first defined.

Is "use tuples instead of lists, when possible" a good rule?
Jul 18 '05 #1
2 1252
be*******@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.

Regards,
Martin

Jul 18 '05 #2
In article <c3*************@news.t-online.com>, Martin v. Lwis wrote:
be*******@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. :
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Ivan Voras | last post: by
42 posts views Thread by Jeff Wagner | last post: by
66 posts views Thread by Mike Meyer | last post: by
7 posts views Thread by MTD | last post: by
10 posts views Thread by rshepard | last post: by
12 posts views Thread by rshepard | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.