Python List
Sequences are the most basic data structures in Python. Each element in a sequence is assigned a number - its position, or index. The first index is 0, the second index is 1, and so on.
Python has 6 built-in types for sequences, but the most common are lists and tuples.
The operations that can be performed on all sequences include indexing, slicing, adding, multiplying, and checking membership.
In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.
Lists are the most commonly used Python data type and can appear as comma-separated values within square brackets.
The data items of a list do not need to be of the same type.
To create a list, just enclose different comma-separated data items in square brackets. As shown below:
Like string indexing, list indexing starts at 0. Lists can be sliced, combined, and so on.
Access values in a list
Use subscript indexing to access values in a list. Similarly, you can use square brackets to slice characters, as shown below:
Example (Python 2.0+)
The output of the above example is:
list1[0]: physics list2[1:5]: [2, 3, 4, 5]
Update list
You can modify or update the data items of a list. You can also use the append() method to add list items, as shown below:
Example (Python 2.0+)
Note:We will discuss the use of the append() method in a later chapter.
The output of the above example is:
['Google', 'Example']
Delete list elements
You can use the del statement to delete list elements, as in the following example:
Example (Python 2.0+)
The output of the above example is:
['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]
Note:We will discuss the use of the remove() method in a later chapter.
Python list script operators
Lists use the + and * operators in a similar way to strings. The + sign is used to combine lists, and the * sign is used to repeat lists.
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!'] | Repetition |
| 3 in [1, 2, 3] | True | Whether the element exists in the list |
| for x in [1, 2, 3]: print x, | 1 2 3 | Iteration |
Python list slicing
An example of Python list slicing is as follows:
Description:
| Python expression | Result | Description |
|---|---|---|
| L[2] | 'Taobao' | Read the third element in the list |
| L[-2] | 'Example' | Read the second-to-last element in the list |
| L[1:] | ['Example', '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 elements of two lists |
| 2 | len(list) Number of list elements |
| 3 | max(list) Return the maximum value of list elements |
| 4 | min(list) Return the minimum value of list elements |
| 5 | list(seq) Convert a tuple to 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 the number of times an element appears in the list |
| 3 | list.extend(seq) Append multiple values from another sequence to the end of the list at once (extend the original list with the new list) |
| 4 | list.index(obj) Find the index position of the first matching item 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 element by default) and return the value of that element |
| 7 | list.remove(obj) Remove the first matching item 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 |