Built-in class list

Unlike ranges, lists are mutable — their elements can be modified, new elements can be added, and existing elements can be removed, they can also be sliced and subscripted.

Literals, operators, functions, and methods

Note, if index() cannot find x in s then an error occurs.

Method pop() differs from the related method remove() in that pop()'s argument is an index to remove a value from while remove()'s is a value to remove from the list s.

Method remove() differs from the related method pop() in that remove()'s argument is a value to be removed while pop()'s is an index to remove a value from.

Common usage patterns

To accumulate something across all elements of a list. Suppose s is a list.

# initialize result to 0, 1, [] or '' depending on what is being built

result = ...

# process elements of s one by one

for element in s:

    # examine element to determine its part in the accumulation.

    ...

    # add the computed part to the accumulation.

    ...


Build a new list by converting the elements of an existing list to values of a different type. Suppose s is a list.

# t is to be new list, it has has no elements as of yet

t = []

# process elements of s one by one

for element in s :

    # examine element to determine its conversion value; e.g.,

    # string to number

    value = ...

    # add the value to the accumulation list

    t.append( value )