Python3 list
Sequences are the most basic data structure in Python.
Each value in the sequence has a corresponding position value, called an index, the first index is 0, the second index is 1, and so on.
Python has six built-in types for sequences, but the most common are lists and tuples.
Operations that can be performed on lists include indexing, slicing, adding, multiplying, and checking membership.
Additionally, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.
A list is the most commonly used Python data type, and it can appear as a comma-separated value enclosed in square brackets.
The data items of the list do not need to have the same type
To create a list, simply enclose the different data items separated by commas in square brackets. As follows:
list0 = []
list1 = ['Google', 'Tarzan', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
list4 = list() #创建一个空列表
list5 = list('tarzan')
list6=list([3,4,5])
access the values in the list
Like string indexing, list indexing starts at 0, the second index is 1, and so on.
Operations such as interception and combination can be performed through the index list.
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print( list[0] )
print( list[1] )
print( list[2] )
The output of the above example:
red
green
blue
The index can also start from the end, the index of the last element is -1, the previous one is -2, and so on.
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print( list[-1] )
print( list[-2] )
print( list[-3] )
The output of the above example:
black
white
yellow
Use the subscript index to access the value in the list, and you can also use the form of square brackets [] to intercept characters, as shown below:
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[0:4])
[10, 20, 30, 40]
Use negative index values to truncate:
list = ['Google', 'Tarzan', "Zhihu", "Taobao", "Wiki"]
# 读取第二位
print ("list[1]: ", list[1])
# 从第二位开始(包含)截取到倒数第二位(不包含)
print ("list[1:-2]: ", list[1:-2])
The output of the above example:
list[1]: Tarzan
list[1:-2]: [‘Tarzan’, ‘Zhihu’]
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:
list = ['Google', 'Tarzan', 1997, 2000]
print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])
list1 = ['Google', 'Tarzan', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)
# 添加一个列表元素到列表末尾
list1.append([5, 6])
print ("更新后的列表 : ", list1)
Note : In Python 3, the list.append() method is used to add an element to the end of the list. It takes one argument, the element to add to the end of the list. The list.append() method will add the specified element to the end of the list and return None. If you want to add multiple elements to the end of the list, you can use an iterable object as a parameter in the append() method, such as a list or tuple.
The output of the above example:
第三个元素为 : 1997
更新后的第三个元素为 : 2001
更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu']
更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu',[5, 6]]
delete list element
You can use the del statement or to delete the elements of the list, as in the following example:
list = ['Google', 'Tarzan', 1997, 2000]
print ("原始列表 : ", list)
del list[2]
print ("删除第三个元素 : ", list)
list.remove(2000)
print ("删除2000后列表 : ", list)
The output of the above example:
Original list: ['Google', 'Tarzan', 1997, 2000]
Delete the third element: ['Google', 'Tarzan', 2000]
List after deleting 2000: ['Google', 'Tarzan']
Note : In Python 3, the list.remove() method is used to remove the specified element from the list. It takes one argument, the value of the element to remove. The list.remove() method removes the first matching element found from the list and returns the value of the removed element. If no matching element is found in the list, a ValueError exception is raised.
Python list script operator
The + and * operators for lists 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, end=" ") | 1 2 3 | iteration |
Python list interception and splicing
Python's list interception and string operation types are as follows:
L=['Google', 'Runoob', 'Taobao']
operate:
Python expression | result | describe |
---|---|---|
L[2] | 'Taobao' | read the third element |
L[-2] | 'Runob' | Read the second-to-last element from the right: count from the right |
L[1:] | [‘Runoob’, ‘Taobao’] | output all elements starting from the second element |
>>>L=['Google', 'Tarzan', 'Taobao']
>>> L[2]
'Taobao'
>>> L[-2]
'Tarzan'
>>> L[1:]
['Tarzan', 'Taobao']
>>>
Lists also support the concatenation operation:
>>>squares = [1, 4, 9, 16, 25]
>>> squares += [36, 49, 64, 81, 100]
>>> squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>
nested list
Use nested lists to create other lists inside lists, for example:
>>>a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
list comparison
List comparison needs to introduce the eq method of the operator module:
# 导入 operator 模块
import operator
a = [1, 2]
b = [2, 3]
c = [2, 3]
print("operator.eq(a,b): ", operator.eq(a,b))
print("operator.eq(c,b): ", operator.eq(c,b))
The output of the above code is:
operator.eq(a,b): False
operator.eq(c,b): True
Python List Functions & Methods
method | describe |
---|---|
list.append(obj) | Add new object at the end of the list |
list.count(obj) | Count the number of times an element appears in a list |
list.extend(seq) | Appends multiple values from another sequence at once to the end of a list (extends the original list with the new list) |
list.index(obj) | Find the index position of the first occurrence of a value in a list |
list.insert(index, obj) | insert object into list |
list.pop([index=-1]) | Remove an element in the list (the last element by default), and return the value of the element |
list.remove(obj) | removes the first occurrence of a value in a list |
list.reverse() | reverse list element |
list.sort( key=None, reverse=False) | Sort the original list |
list.clear() | clear the list |
list.copy() | copy list |