First page Back Continue Last page Overview Graphics

Nested Lists

Lists can contain other lists (sublists):

>>> birthdays = [("Marc","12/10/67"), ("Steve","3/4/91")]

>>>

>>> marc = birthdays[0]

>>> marc

('Marc', '12/10/67')

>>>

>>> print("Marc's birthday: " + birthdays[0][1])

Marc's birthday: 12/10/67

What about the following list?

>>> x = [42]

>>> x.append(x)

Attention:

>>> a = [3,5]

>>> x = []

>>> y =[]

>>> x.append(a)

>>> y.append(a)

>>> y

[[3, 5]]

>>> x

[[3, 5]]

>>> a.append(42)

>>> x

[[3, 5, 42]]

>>> y

[[3, 5, 42]]