By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

tuples or lists

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.