Lists¶
Python has a powerful built-in list type:
1 []
2 [1]
3 [1, "2.", 3.0, ["4a", "4b"], (5.1,5.2)]
A list can contain a mixture of other types as elements, including strings, tuples, lists, dictionaries, functions, file objects and any kind of number.
A list can be indexed from the front or the back. You can also refer to a sub-segment of a list using slice notation:
1 >>> x = [1, "2.", 3.0, ["4a", "4b"], (5.1,5.2)]
2 >>> x[0]
3 '1'
4 >>> x[1]
5 '2.'
6 >>> x[-1]
7 (5.1, 5.2)
8 >>> x[-2]
9 ['4a', '4b']
10 >>> x[1:-1]
11 ['2.', 3.0, ['4a', '4b']]
12 >>> x[0:3]
13 [1, '2.', 3.0]
14 >>> x[:3]
15 [1, '2.', 3.0]
16 >>> x[-4:-1]
17 ['2.', 3.0, ['4a', '4b']]
18 >>> x[-4:]
19 ['2.', 3.0, ['4a', '4b'], (5.1, 5.2)]
- Lines 2 and 4
Index from the beginning using positive indices starting with
0
as the first element.- Lines 6 and 8
Index from the back using negative indices starting with
-1
as the last element.- Lines 10 and 12
Slice with
[m:n]
, wherem
is the inclusive start point andn
is the exclusive end point.- Lines 14, 16 and 18
A
[:n]
slice starts at the beginning and an[m:]
slice goes to the end of a list.
You can use this notation to add, remove and replace elements in a list or to get an element or a new list that is a slice of it, for example:
1 >>> x = [1, "2.", 3.0, ["4a", "4b"], (5.1,5.2)]
2 >>> x[1] = "zweitens"
3 >>> x[2:3] = []
4 >>> x
5 [1, 'zweitens', ['4a', '4b'], (5.1, 5.2)]
6 >>> x[2] = [3.1, 3.2, 3.3]
7 >>> x
8 [1, 'zweitens', [3.1, 3.2, 3.3], (5.1, 5.2)]
9 >>> x[2:]
10 [[3.1, 3.2, 3.3], (5.1, 5.2)]
- Line 3
The size of the list increases or decreases if the new slice is larger or smaller than the slice it replaces.
Slices also allow a step-by-step selection between the start and end indices.
The default value for an unspecified stride is 1
, which takes every element
from a sequence between the indices. With a stride of 2
, every second
element is taken and so on:
1>>> x[0:3:2]
2[1, [3.1, 3.2, 3.3]]
3>>> x[::2]
4[1, [3.1, 3.2, 3.3]]
5>>> x[1::2]
6['zweitens', (5.1, 5.2)]
The stride value can also be negative. A -1
stride means counting from right
to left:
1>>> x[3:0:-2]
2[(5.1, 5.2), 'zweitens']
3>>> x[::-2]
4[(5.1, 5.2), 'zweitens']
5>>> x[::-1]
6[(5.1, 5.2), [3.1, 3.2, 3.3], 'zweitens', 1]
- Line 1
To use a negative increment, the start slice should be larger than the end slice.
- Line 3
The exception is if you omit the start and end indices.
- Line 5
A stride of
-1
reverses the order.
Some functions of the slice notation can also be executed with special operations, which improves the readability of the code:
1>>> x.reverse()
2>>> x
3[(5.1, 5.2), [3.1, 3.2, 3.3], 'zweitens', 1]
You can also use the following built-in functions (len
, max
and
min
), some operators (in
, +
and *
), the del
statement and
the list methods (append
, count
, extend
, index
, insert
,
pop
, remove
, reverse
and sort
) for lists:
1 >>> len(x)
2 4
3 >>> x + [0, -1]
4 [(5.1, 5.2), [3.1, 3.2, 3.3], 'zweitens', 1, 0, -1]
5 >>> x.reverse()
6 >>> x
7 [1, 'zweitens', [3.1, 3.2, 3.3], (5.1, 5.2)]
- Line 3
The operators
+
and*
each create a new list, leaving the original list unchanged.- Line 5
The methods of a list are called using the attribute notation for the list itself:
:samp:`{LIST}.METHOD(ARGUMENTS)
.
Some of these operations repeat functions that can be performed using slice notation, but they improve the readability of the code.
See also
Summary¶
data type |
mutable |
ordered |
indexed |
duplicates |
---|---|---|---|---|
list |
✅ |
✅ |
✅ |
✅ |