Python Lists (List)
A sequence is the most basic data structure in Python. Each element in a sequence is assigned a number - its position, or index. The first index is 0, the second is 1, and so on.
Python has six built-in sequence types, but the most common are lists and tuples.
Operations that can be performed on sequences include indexing, slicing, addition, multiplication, and membership checking.
In addition, Python has built-in methods for determining the length of a sequence and finding the maximum and minimum elements.
A list is the most commonly used Python data type; it can appear as a comma-separated list of values within square brackets.
The data items of a list need not have the same type
To create a list, simply enclose comma-separated data items in square brackets, as shown below:
list1
=
[
'
physics
'
,
'
chemistry
'
,
1997
,
2000
]
list2
=
[
1
,
2
,
3
,
4
,
5
]
list3
=
[
"
a
"
,
"
b
"
,
"
c
"
,
"
d
"
]
Like string indexing, list indexing starts at 0. Lists can be sliced, combined, and so on.
Accessing values in a list
Use subscript indexing to access values in a list, and you can also use square brackets to slice characters, as shown below:
Example (Python 2.0+)
#!/usr/bin/python
list1
=
[
'
physics
'
,
'
chemistry
'
,
1997
,
2000
]
list2
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
]
"
list1[0]:
"
,
list1
[
0
]
"
list2[1:5]:
"
,
list2
[
1
:
5
]
The example above produces the following output:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Updating a list
You can modify or update the data items in a list, and you can also use the append() method to add list items, as shown below:
Example (Python 2.0+)
#!/usr/bin/python
-- coding: UTF-8 --
list
=
[
]
Empty list
list
.
append
(
'
'
)
Use append() to add elements
list
.
append
(
'
Runoob
'
)
list
**Note:** We will discuss the use of the append() method in the following chapters
The example above produces the following output:
['Google', 'Runoob']
Deleting list elements
You can use the del statement to delete elements of a list, as in the following example:
Example (Python 2.0+)
#!/usr/bin/python
list1
=
[
'
physics
'
,
'
chemistry
'
,
1997
,
2000
]
list1
del
list1
[
2
]
"
After deleting value at index 2 :
"
list1
The example above produces the following output:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
**Note:** We will discuss the use of the remove() method in the following chapters
Python list script operators
The + and * operators on lists are similar to those on strings. + combines lists and * repeats a list.
As shown below:
| Python expression | Result | Description |
|---|---|---|
| len([1, 2, 3]) | 3 | Length |
| [1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | Combination |
| ['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | Repeat |
| 3 in [1, 2, 3] | True | Whether an element exists in the list |
| for x in [1, 2, 3]: print x, | 1 2 3 | Iterate |
Python List Slicing
Examples of Python list slicing are as follows:
L
=
[
'
'
,
'
Runoob
'
,
'
Taobao
'
]
L
[
2
]
'
Taobao
'
L
[
2
]
'
Runoob
'
L
[
1
:
]
[
'
Runoob
'
,
'
Taobao
'
]
Description:
| Python expression | Result | Description |
|---|---|---|
| L[2] | 'Taobao' | Read the third element of the list |
| L[-2] | 'Runoob' | Read the second-to-last element of the list |
| L[1:] | ['Runoob', 'Taobao'] | Slice the list starting from the second element |
Python list functions & methods
Python includes the following functions:
| No. | function |
|---|---|
| 1 | cmp(list1, list2) |
| Compare the elements of two lists | |
| 2 | len(list) |
| Number of elements in the list | |
| 3 | max(list) |
| Return the maximum value of the list elements | |
| 4 | min(list) |
| Return the minimum value of the list elements | |
| 5 | list(seq) |
| Convert a tuple into a list |
Python includes the following methods:
| No. | Method |
|---|---|
| 1 | list.append(obj) |
| Add a new object at the end of the list | |
| 2 | list.count(obj) |
| Count how many times an element appears in the list | |
| 3 | list.extend(seq) |
| Append multiple values from another sequence at once at the end of the list (extend the original list with a new list) | |
| 4 | list.index(obj) |
| Find the index of the first match of a value in the list | |
| 5 | list.insert(index, obj) |
| Insert an object into the list | |
| 6 | list.pop([index=-1]) |
| Remove an element from the list (the last one by default) and return its value | |
| 7 | list.remove(obj) |
| Remove the first match of a value in the list | |
| 8 | list.reverse() |
| Reverse the elements in the list | |
| 9 | list.sort(cmp=None, key=None, reverse=False) |
| Sort the original list |