Python List (List)
Sequences are the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, where 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.
Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.
Additionally, 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, which can appear as comma-separated values within square brackets.
The data items of the list do not need to have the same type
To create a list, simply enclose the comma-separated distinct data items in square brackets. As follows:
list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"]
Like indices for strings, list indices start at 0. The list can be intercepted, combined, etc.
access the values in the list
Use subscript indexing to access values in the list, and you can also use square brackets to truncate characters, like this:
Example (Python 2.0+)
#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]
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 the list, and you can also use the append() method to add list items, as follows:
Example (Python 2.0+)
#!/usr/bin/python # -*- coding: UTF-8 -*- list = [] ## empty list list.append('Google') ## adding elements using append() list.append('Runoob ') print list
Note: We will discuss the use of the append() method in the next chapter
The output of the above example is:
['Google', 'Runoob']
remove list element
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] print list1 del list1[2] print "After deleting value at index 2 : " print list1
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 the next chapter
Python List Script Operator
List operators for + and * are similar to strings. The + sign is used to combine lists, and the * sign is used to repeat lists.
As follows:
Python expression | result | describe |
---|---|---|
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 the element exists in the list |
for x in [1, 2, 3]: print x, | 1 2 3 | iterate |
Python list interception
An example of list interception in Python is as follows:
>>>L = ['Google', 'Runoob', 'Taobao'] >>> L[2] 'Taobao' >>> L[-2] 'Runoob' >>> L[1:] ['Runoob', 'Taobao'] >>>
describe:
Python expression | result | describe |
---|---|---|
L[2] | 'Taobao' | Read the third element in the list |
L[-2] | 'Runoob' | Read the second-to-last element in the list |
L[1:] | ['Runoob', 'Taobao'] | Truncate the list starting from the second element |
Python List Functions & Methods
Python includes the following functions:
serial number | function |
---|---|
1 | cmp(list1, list2) compares the elements of two lists |
2 | len(list) number of list elements |
3 | max(list) returns the maximum value of the list element |
4 | min(list) returns the minimum value of the list element |
5 | list(seq) convert tuple to list |
Python includes the following methods:
serial number | method |
---|---|
1 | list.append(obj) adds a new object at the end of the list |
2 | list.count(obj) counts the number of times an element appears in the list |
3 | list.extend(seq) appends multiple values from another sequence at the end of the list (extends the original list with the new list) |
4 | list.index(obj) finds the index position of the first occurrence of a value from a list |
5 | list.insert(index, obj) inserts the object into the list |
6 | list.pop(obj=list[-1]) removes an element in the list (the last element by default) and returns the value of the element |
7 | list.remove(obj) removes the first occurrence of a value in the list |
8 | list.reverse() reverses the elements in the list |
9 | list.sort([func]) sorts the original list |