Skip to main content

6.5) List Methods


Like strings, lists have methods that operate on them.

You can add an item to a list:

>>> planets=['Mercury','Venus','Earth','Mars']
>>> planets.append('Jupiter')
>>> planets
['Mercury','Venus','Earth','Mars','Jupiter']

The method append() directly modifies the list. The same is true for the function sort():

>>> planets=['Mercury','Venus','Earth','Mars']
>>> planets.sort()
>>> planets
['Earth', 'Mars', 'Mercury', 'Venus']

The list of planets is now sorted in alphabetical order, so we've lost the original order of the planets.