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 thenlen(s)
is the number of elements ins
.
- If
s
is a list thendel s[a:b]
removes the elements froms
with indicesa
throughb-1
.
- The
+
operator can combine two lists to produce a new list. Ifs
andt
are lists, thenu = s + t
makesu
a new list. The number of elements inu
islen(s) + len(t)
. Listu
corresponds to the list of values ins
concatenated with the list of values int
.
- The
append()
method adds a new element to the end of a list. Ifs
is a list, thens.append(x)
grows the size ofs
by one by appending an elementx
to the end of lists
.
- Method
clear()
can clear out the elements of a list. Ifs
is a list, thens.clear()
removes all elements froms
.
- Method
copy()
produces a copy of its list. Ifs
is a list, thens.copy()
returns a new list, which has the same values ass
.
- Method
count()
counts occurrences in a list. Ifs
is a list, thens.count(x)
returns the number of elements ins
equal tox
.
- The
in
andnot in
operators determine whether values are or are not part of a list. Ifs
is a list, thenx in s
isTrue
whenx
is one of the elements ofs
, andx not in s
is true whenx
is not one of the elements ofs
.
- The
index()
method has several forms for inspecting a list. Supposes
is a list,x
is a value, andn
is the length ofs
(i.e.,n = len(s)
.
s.index(x)
determines the index of the first occurrence ofx
in lists
.
s.index(x, i)
determines the index of the first occurrence ofx
in lists
among the elements with indicesi ... n-1
.
s.index(x, i, j)
determines the index of the first occurrence ofx
in lists
among the elements with indicesi ... j-1
.
Note, if
index()
cannot findx
ins
then an error occurs.
- The
insert()
method inserts a new element into a list. Ifs
is a list, thens.insert(a, x)
grows the size ofs
by one by inserting a new elementx
intos
at indexa
.
- The
pop()
method removes an element from a list and retrieve its value. There are two forms of thepop()
method. Supposes
is a list.
s.pop()
returns the last element ins
and also removes it from the lists
.
s.pop(i)
returns the element with indexi
from its lists
and also removes it from the lists
.
Method
pop()
differs from the related methodremove()
in thatpop()
's argument is an index to remove a value from whileremove()
's is avalue
to remove from the lists
.
- The
remove()
method can be used to remove a value from a list. Ifs
is a list, thens.remove(x)
removes the first occurrence ofx
froms
.
Method
remove()
differs from the related methodpop()
in thatremove()
's argument is a value to be removed whilepop()
's is anindex
to remove a value from.
- The
reverse()
method can be used to reverse values in a list. Ifs
is a list, thens.reverse()
reverses the order of element values ins
.
- Method
sort()
puts its list into sorted order. Ifs
is a list, thens.sort()
rearranges the elements ofs
into non-descending order.
- The built-in function
max()
returns the "largest" value in a list. Ifs
is a list,max(s)
is an element ofs
that has the largest value. Functionmax()
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. Ifs
is a list,min(s)
is an element ofs
that has the smallest value. Functionmin()
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. Ifs
is a list, thensorted(s)
returns a new list whose elements are those ofs
but in sorted order. Functionsorted()
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 )