4 ms·
> Lists are largely for homogeneous collections, whereas tuples are heterogeneous. What? What makes you think that?
by snth 13y ago
> Lists are largely for homogeneous collections, whereas tuples are heterogeneous.
What? What makes you think that?
- Myrmornis 13y agoIt's a perfectly accurate statement. If a tuple is homogeneous, it's only because all of its slots happen to be for objects of the same type (e.g. a row of a database table all of whose columns are integers, or the arguments of a function which takes 3 integers as input). In contrast, homogeneous lists are the norm (other than mixing None with non-None values). When was the last time you needed a heterogeneous list?
- wahnfrieden 13y agoThe quote comes from Guido van Rossum almost verbatim so that may be a good place to start if you want to dig deeper. As I've also seen, a good rephrasing is that lists have order whereas tuples have structure. Yet another mnemonic I've heard that seems apt is that tuples are appropriate where a named-tuple could also conceivably be used.
- beambot 13y agoThat's crazy-interesting and seems like a very good rule of thumb. I've been using Python for a very long time and had never heard this. Why is it not in the PEP?!
- wahnfrieden 13y agoWhich PEP, PEP-8? That's only a style guide. It's included in the stdlib documentation on data structures: http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences http://docs.python.org/2/tutorial/datastructures.html#tuples...
- dalke 13y agoThe quote is "Tuples are for heterogeneous data, list are for homogeneous data. Tuples are not read-only lists." Guido van Rossum March 12, 2003 in python-dev. However, don’t treat Guido’s statement as a mandate! "Yes, taking Guido too seriously can have that effect on people <1.1 wink> The trick is in knowing when Guido is proscribing dogmatic law, and when he is describing his intent when he designed feature(tte)s of Python, or his point of view at this moment, or how he wished people would see it, or reacting to what he had for lunch. He's just human, you know, and no more infallible than the best of us." Thomas Wouters March 12, 2003 in python-dev For example, consider object.__class__.__bases__. That's a tuple of homogenous data. It's not of a fixed length, and the base class at a given position has no intrinsic name, other than "first", "second", etc. Do you think the use of tuple here is a bug? Should it be a list instead?
- wahnfrieden 13y agoGreat follow-up, thanks! It's not a clear and consistent law, or law at all, you're right.
- wahnfrieden 13y agoI can't edit my other answer, so I'm adding this here since you sound so convinced I'm delusional ;) GvR aside, even the documentation on python.org mentions this: http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences http://docs.python.org/2/tutorial/datastructures.html#tuples...
- snth 13y agoHa! How about that. Thanks for the link.
- kyllo 13y agoExample of a typical list: [1,2,3,4,5] Example of some typical tuples: (1,'A') (2,'B') (3,'C') In Python, of course lists can be heterogenous, so you could have a list like [1, 'A', 2, 'B', 3, 'C'] but it would be an atypical/un-idiomatic use case. A list of tuples or possibly a dict would make more sense for that.
- dragonwriter 13y agoIn a dynamically typed language with a type heirarchy with a common root, it is impossible to distinguish between a heterogenous list whose entities are instances of a variety of specific types and a homogenous list whose members are all members of one of the common supertypes of the specific types of the objects. With a language that uses duck typing (so that the meaningful "types" of objects are often not their explicit classes but the implicit -- both in definition and in association with particular objects -- interfaces expected by the code which accesses them), this ambiguity is even more pronounced.
- kyllo 13y agoThis is true, and I'm sure Guido is fully aware of it, but it's also not entirely relevant to what he was saying. Regardless of inheritance, interfaces, and duck typing, the explicit classes of the objects still do matter. Yes, the list of [1,'A',2,'B',3,'C'] is really after all just a list of [Object, Object, Object, Object, Object, Object]. And the list itself is also just an Object. But the explicit classes of the elements of the list are different ([int,str,int,str,int,str]), and often that actually does matter for the use case, because int and str have different interfaces, and "adding" ints means something different than "adding" strs for example. It really just depends on what you're going to do with that data, and I think Guido was also talking in terms of the idiomatic use cases for the different data structures.
- dragonwriter 13y ago> This is true, and I'm sure Guido is fully aware of it Well, yeah. I'd be kind of stunned if he wasn't. I was making a note qualifying the examples that were given in isolation, not questioning the basic premise. > Regardless of inheritance, interfaces, and duck typing, the explicit classes of the objects still do matter. Whether or not that's true depends on the use case; the really relevant sense of homogenous or heterogenous that distinguishes the idiomatic use of tuples vs. lists is contextual, and is more about the interfaces that the consuming code expects the members to provide than the explicit classes per se. There's nothing unidiomatic in Python about using a list for a series of items of different explicit classes that are going to have the same operations (supported by a common interface they all share) performed on them.