Is the List in python homogeneous or heterogeneous?
6 Answers
>>> def a(): pass
>>> lst=[1,'one',{1:'one'},a,[1,1],(1,),True,set((1,))]
>>> for each in lst:
... print type(each), str(each)
...
<type 'int'> 1
<type 'str'> one
<type 'dict'> {1: 'one'}
<type 'function'> <function a at 0x100496938>
<type 'list'> [1, 1]
<type 'tuple'> (1,)
<type 'bool'> True
<type 'set'> set([1])
Any questions?
7 Comments
Do you realize that it's exactly the contrary of what your above answer seems to express. It is not contrary at all. Lists are an ordered, homogeneous, muteable collection of Python objects; objects in Python can be almost anything at all. Since Python objects are various types, a homogeneous list of various types can appear heterogeneous. You just need to truly grok what a Python object refers to: almost anything that is actionable...Lists in Python can be heterogeneous, but by the general convention it is preferable that they only contain homogeneous elements. Python tuples are the natural data structure for heterogeneous sequences.
Of course you can argue that both tuples and lists are just homogeneous sequences of Python objects, but that is a misleading oversimplification and doesn't add any value.
Since tuples are immutable and lists are mutable you could also argue that mutability is the real distinction between them. However, that is not how they were intended. More on this can be found in this question.
1 Comment
collections.NamedTuple it became obvious that numerical indexing of tuples was less readable.The List in Python is heterogeneous - the same list can accept objects of various different types.
There is a snippet here which gives you a homogeneous list in Python. No idea how that piece of code would perform however.
Comments
Please try a simple
a=[1, "a"]
and see if it throws an error before asking a question.
Btw, it does not.
2 Comments
It depends if you talk about the direct elements of a list or its indirect elements.
I believe that a list is in reality a list of references, not of objects, and that when the interpeter has to use a list, it knows that in fact it must manipulate the objects to which the references are pointing, not the references themselves: that's the "execution model" of Python, I think.
What I call indirect element of a list are the objects that constitute the value of the list.
What I call direct elements are the references to these objects that constitutes the implementation of the list.
So:
considered as a list of references, a list is homogenous: all the elements are variables, in the plain sense of variable = chunk of memory whose value can change (while the value of an object never changes)
considered as a list of the objects pointed by the references, a list is heterogenous. The innuendo being that their TYPES are heterogenous.
That's my understanding. I don't know if I am fully right.
3 Comments
I am not at my ease with the meaning they give to the word 'object'. Without a clear understanding of "object" as used by Python, you will be very confused by many of the concepts of the language. It is core concept. I don't understand why you evoke the passing or arguments to functions... The discussion regarding objects is germane to the understanding of lists as a collection of objects.One more argument for homogeneity of lists: the PEP 484 and the new typing module. The items of tuple can be individually typed with Tuple:
Tuple, used by listing the element types, for exampleTuple[int, int, str]. The empty tuple can be typed asTuple[()]. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for exampleTuple[int, ...]. (The ... here are part of the syntax, a literal ellipsis.) [reference]
>>> import typing
>>> typing.Tuple[str, int, float]
typing.Tuple[str, int, float]
However a list, via typing.List only accepts a single type parameter:
List, used asList[element_type]
>>> typing.List[str, int, float]
Traceback (most recent call last):
...
TypeError: Too many parameters for typing.List<~T>; actual 3, expected 1
It's a deceitful answer. I would downvote if it was possible.That seems a bit harsh. Why do you think this answer is deceitful? It is 100% correct AFAK. If the elements in a Python list are not objects, what are they in Python speak?