10

In python documentation list is defined as:

mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).

Why it's used to store collections of homogeneous items?

Are a string and an int item also homogeneous then?

a = [12,"hello"]
user2864740
  • 60,010
  • 15
  • 145
  • 220
Aidin.T
  • 731
  • 3
  • 10
  • 25
  • It's the dictionary term: "of the same type" – user2864740 Nov 01 '13 at 19:07
  • 6
    Strange choice of wording. The only thing homogeneous I can see here is that they are python objects. – wim Nov 01 '13 at 19:11
  • 1
    Related: [python: list vs tuple, when to use each?](http://stackoverflow.com/questions/1708510/python-list-vs-tuple-when-to-use-each) – Ashwini Chaudhary Nov 01 '13 at 19:13
  • 1
    "Typically" used. The most common use of a list is to iterate over it, so it would be expected that each `i` in `for i in myList` provides the same functionality. Also, the homogeneity of a list would be determined by what functionality is expected of the the items. If the only requirement is that `str(i)` return a usable string, `12` and `"hello"` could well be considered the same "type". – chepner Nov 01 '13 at 19:29

3 Answers3

12

Homogeneous means "of the same or a similar kind or nature".

While any value can be stored in a list along with any other value, in doing so the definition of "kind or nature" must be widened when dealing with the sequence. During this widening (or "unification"), the set of operations that can be performed upon every item in the sequence becomes the "lowest common set of operations" shared between all items.

This is why "[list are] typically used to store collections of homogeneous items" - so the items in the sequence can be treated with an appropriate level of unification:

# a list of animals that can "speak"
animals = [Dog(), Cat(), Turkey()]
for a in animals:
  a.speak()

# .. but a string cannot "speak"
animals = [Dog(), "Meow!", Turkey()]
user2864740
  • 60,010
  • 15
  • 145
  • 220
2

While you technically can store any object in a list:

[12, "hello", list, list()]

Lists are, as the documentation says, usually used to store similar items:

[12, 24, 99]
["hello", "goodbye"]
[list, dict, int]

The meaning of "homogenous" is simply "similar".

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
2

It talks about a common use case (that's why it says "typically"). Homogeneity is neither expected nor enforced, as illustrated by the example in your question. Even what it means for items to be "homogenous" is not precisely defined: the doc says that this "will vary by application".

NPE
  • 486,780
  • 108
  • 951
  • 1,012