I have a question about lists: why does Prof Guttag insist on them being heterogeneous, when tuples too can contain objects of different types? Is it only recommended for style, or is there actually a difference? Also, related to this question, why does range return a list and not a tuple? Is there a difference in terms of efficiency? Lastly, what are the actual differences between lists and tuples besides mutability? Or more concretely, (again putting aside the question of mutability) are there cases where you absolutely don't want to use one instead of the other?
Tuple are good for very small collections that won't change. They can even be used as dictionary keys. I only really use them for doing things like returning multiple values from a function. You almost always want to use a list. Range is not efficient for large numbers, and xrange should be used instead. Range returns a list because it was intended to be a list. There is no reason for range to return a tuple.
Thank you for your answer! And what about the heterogeneous thing? I've also read on other sites that tuples are homogeneous, but hm, I can do the exact same things as I do with lists, that is, I can create a tuple containing a string, a number, a tuple, a list, etc. So when the prof or other people say lists are hetereogeneous and tuples homogeneous, are they more saying something like "when you want to make a collection of homogeneous objects it's better (recommended, good style, etc.) to use a tuple than a list", or is there an actual, technical reason for that?
this seems to answer your question, concluding that there is no need for either lists or tuples to be homogeneous or heterogeneous, but that it is essentially a matter of convention. http://stackoverflow.com/questions/17764978/whats-the-difference-between-homogeneous-and-heterogeneous-sequences-in-python
Thank you! And now I realize the little mix-up I've made above: the convention is list = homo- and tuples = hetero-. But the prof did characterize lists as being hetero- (he insists on the fact that they're great because you can put everyting in them). I think what I'll take from that is that as you said, you can do pretty much what you want with them, but I should probably stick to the convention (if only to not confuse other programmers who would read my code). Thank you both again.
Join our real-time social learning platform and learn together with your friends!