In Python, the List is one type of homogeneous sequences, while the Tuple is one type of heterogeneous sequences. But in a List, we still can put arbitrary type of data in it, like a=[1, 2, 'abc']
. So, what's the real difference between homogeneous and heterogeneous sequences in Python?
Lists and tuples are mostly identical in Python, except that lists are mutable and tuples are immutable. Both lists and tuples can be either homogeneous or heterogeneous.
If you want sequences with enforced homogeneity, use the array
module or use NumPy, for example.
From the Python Documentation for sequence types:
Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).
Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With