## 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

- The empty list literal is
`[]`

- If
`s`

is a list then`len(s)`

is the number of elements in`s`

.

- If
`s`

is a list then`del s[a:b]`

removes the elements from`s`

with indices`a`

through`b-1`

.

- The
`+`

operator can combine two lists to produce a new list. If`s`

and`t`

are lists, then`u = s + t`

makes`u`

a new list. The number of elements in`u`

is`len(s) + len(t)`

. List`u`

corresponds to the list of values in`s`

concatenated with the list of values in`t`

.

- The
`append()`

method adds a new element to the end of a list. If`s`

is a list, then`s.append(x)`

grows the size of`s`

by one by appending an element`x`

to the end of list`s`

.

- Method
`clear()`

can clear out the elements of a list. If`s`

is a list, then`s.clear()`

removes all elements from`s`

.

- Method
`copy()`

produces a copy of its list. If`s`

is a list, then`s.copy()`

returns a new list, which has the same values as`s`

.

- Method
`count()`

counts occurrences in a list. If`s`

is a list, then`s.count(x)`

returns the number of elements in`s`

equal to`x`

.

- The
`in`

and`not in`

operators determine whether values are or are not part of a list. If`s`

is a list, then`x in s`

is`True`

when`x`

is one of the elements of`s`

, and`x not in s`

is true when`x`

is not one of the elements of`s`

.

- The
`index()`

method has several forms for inspecting a list. Suppose`s`

is a list,`x`

is a value, and`n`

is the length of`s`

(i.e.,`n = len(s)`

.

`s.index(x)`

determines the index of the first occurrence of`x`

in list`s`

.

`s.index(x, i)`

determines the index of the first occurrence of`x`

in list`s`

among the elements with indices`i ... n-1`

.

`s.index(x, i, j)`

determines the index of the first occurrence of`x`

in list`s`

among the elements with indices`i ... j-1`

.

Note, if

`index()`

cannot find`x`

in`s`

then an error occurs.

- The
`insert()`

method inserts a new element into a list. If`s`

is a list, then`s.insert(a, x)`

grows the size of`s`

by one by inserting a new element`x`

into`s`

at index`a`

.

- The
`pop()`

method removes an element from a list and retrieve its value. There are two forms of the`pop()`

method. Suppose`s`

is a list.

`s.pop()`

returns the last element in`s`

and also removes it from the list`s`

.

`s.pop(i)`

returns the element with index`i`

from its list`s`

and also removes it from the list`s`

.

Method

`pop()`

differs from the related method`remove()`

in that`pop()`

's argument is anindexto remove a value from while`remove()`

's is a`value`

to remove from the list`s`

.

- The
`remove()`

method can be used to remove a value from a list. If`s`

is a list, then`s.remove(x)`

removes the first occurrence of`x`

from`s`

.

Method

`remove()`

differs from the related method`pop()`

in that`remove()`

's argument is avalueto be removed while`pop()`

's is an`index`

to remove a value from.

- The
`reverse()`

method can be used to reverse values in a list. If`s`

is a list, then`s.reverse()`

reverses the order of element values in`s`

.

- Method
`sort()`

puts its list into sorted order. If`s`

is a list, then`s.sort()`

rearranges the elements of`s`

into non-descending order.

- The built-in function
`max()`

returns the "largest" value in a list. If`s`

is a list,`max(s)`

is an element of`s`

that has the largest value. Function`max()`

requires the elements of the list to be comparable (usually meaning they all have the same type).

- The built-in function
`min()`

returns the "smallest" value in a list. If`s`

is a list,`min(s)`

is an element of`s`

that has the smallest value. Function`min()`

requires the elements of the list to be comparable (usually meaning they all have the same type).

- The built-in function
`sorted()`

returns a new list whose elements are in sorted order. If`s`

is a list, then`sorted(s)`

returns a new list whose elements are those of`s`

but in sorted order. Function`sorted()`

requires the elements of the list to be comparable (usually meaning they all have the same type).

### 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 )