Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between homogeneous and heterogeneous sequences in Python? [duplicate]

Tags:

python

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?

like image 341
injoy Avatar asked Jul 20 '13 17:07

injoy


1 Answers

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.

Documentation

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).

like image 117
Dietrich Epp Avatar answered Oct 24 '22 02:10

Dietrich Epp