Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "homogenous" in Python list documentation?

Tags:

python

list

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"]
like image 897
Aidin.T Avatar asked Nov 01 '13 19:11

Aidin.T


1 Answers

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()]
like image 79
user2864740 Avatar answered Sep 23 '22 08:09

user2864740