List related operations in Python


1. Overview of List List

List List is very powerful. It is one of the important data structures of Python. Next, let’s talk about the operations related to lists in an easy-to-understand manner. Including: list update, adding elements, deleting elements, finding elements, and other functions of the list.

2. List update

A single element of a list can be obtained through indexing, or an element can be updated through indexing. The usage method is as convenient as variable assignment.

grammar

list[index]

Parameter
index – the index position of the insertion.

Case

lis1=[1,2,3,4,5]
print(lis1)

lis1[1]='hello python'
print(lis1)

#输出结果
'''
[1, 2, 3, 4, 5]
[1, 'hello python', 3, 4, 5]
'''

Case analysis Judging
from the execution results, lists can store different types of data, and the modified new elements do not need to be consistent with the original element types. However, it should be noted that the index of the updated list must be an existing index, and elements cannot be updated for indexes that exceed the length of the list.

3. Add elements

Elements cannot be added to the list through the index. The index can only modify and update existing elements. If you want to add new elements, you can use the append, extend, and insert functions.

1.
When the append function append() is added, the added elements will be added as a whole, allowing any type of elements to be added.

grammar

list.append(element)

Parameter
element – ​​the element to be inserted into the list.

Return value
This function has no return value, but adds an element to the end of the list.

Case

lis1=[1,2,3,4,5]
print(lis1)

lis1.append('hello python')
print(lis1)

#输出结果
'''
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 'hello python']
'''

Case analysis Judging
from the execution results, append adds a new element directly to the end of the original list. But please note that append can only add one element at a time. If you want to add multiple elements, you must use the extend function.

2.
When the extend function extend() is added, the added data will be added iteratively, and only iterable object data is allowed to be added (iterable objects: objects that can be iterated with a for loop are iterable objects, such as: strings, lists , ancestral, dictionary, collection, etc.).

grammar

list.append(element)

Parameter
element – ​​the element to be inserted into the list.

Return value
This function has no return value, but adds one or more elements to the end of the list.

Case

lis1=[1,2,3,4,5]
print(lis1)

print('*'*50)

lis1.append(['i like','hello python'])
print('使用append结果为:')
print(lis1)

print('*'*50)

lis2=[1,2,3,4,5]
lis2.extend(['i like','hello python'])
print('使用extend结果为:')
print(lis2)
#输出结果
'''
[1, 2, 3, 4, 5]
**************************************************
使用append结果为:
[1, 2, 3, 4, 5, ['i like', 'hello python']]
**************************************************
使用extend结果为:
[1, 2, 3, 4, 5, 'i like', 'hello python']
'''

Case analysis Judging
from the execution results, the functions of append and extend have different functions. Whether it is a single element or a list after append, it will be appended to the end of the original list as a new element. And extend will split the new list and append it to the end of the original list.
The append() and extend() functions both add data to the end of the list. append() supports adding all data, and extend() only supports data of iterable objects.

3. The insert function
insert() is used to insert the specified element into the specified position of the list.

grammar

list.insert(index, element)

Parameter
index – the index position of the element to be inserted.
element – ​​The element to be inserted into the list.

Return value
This function has no return value, but will insert an element at the specified position in the list.

Case

lis1=[1,2,3,4,5]
print(lis1)

print('*'*50)

lis1.insert(2,'hello python')
print('使用insert结果为:')
print(lis1)

#输出结果
'''
[1, 2, 3, 4, 5]
**************************************************
使用insert结果为:
[1, 2, 'hello python', 3, 4, 5]
'''

Case analysis
The insert function needs to pass 2 parameters. The first parameter indicates the position of the new element to be inserted. The second parameter represents the new element to be inserted. Insert is the same as append, only one element can be inserted at a time.

4. Delete elements

Elements can be added, and elements can naturally be deleted. Python has the following methods to delete elements from a list: pop, remove, and del.

1. The pop function
pop() is used to remove an element from the list (the default is the last one in the list) and return the value of the element.

grammar

list.pop(index)

Parameter
index – the index position of the element to be deleted. If empty, the default is to delete the last element of the list.

Return value
This function has a return value, and the return value is the deleted element.

Case

lis1=[1,2,3,4,5]
print('初始列表为:',lis1)

print('*'*50)

res1=lis1.pop()
print('删除元素为:',res1)
print('使用pop结果为:',lis1)

print('-'*50)

lis2=[1,2,3,4,5]
print('初始列表为:',lis2)

print('*'*50)

res2=lis2.pop(2)
print('删除元素为:',res2)
print('使用pop结果为:',lis2)


#输出结果
'''
初始列表为: [1, 2, 3, 4, 5]
**************************************************
删除元素为: 5
使用pop结果为: [1, 2, 3, 4]
--------------------------------------------------
初始列表为: [1, 2, 3, 4, 5]
**************************************************
删除元素为: 3
使用pop结果为: [1, 2, 4, 5]
'''

Case analysis Judging
from the execution results, the pop function can delete the element at the specified position and return this element as the return value. If the position is not specified, the last element of the list is selected by default.

2. The remove function
deletes elements based on their content. What is deleted is the element that first appears in the list.

grammar

list.remove(element)

Parameter
element - the element in the list to be deleted.

Return value
This function has no return value.

Case

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu']
print('初始列表为:',lis1)

print('*'*50)

lis1.remove('wangwu')
print('使用remove结果为:',lis1)

#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
**************************************************
使用remove结果为: ['zhangsan', 'lisi', 'zhaoliu', 'wangwu']
'''

Case study
The remove function will delete the first element found and has no return value.

3. The del function
deletes one or several consecutive elements. This function is used to delete elements.

Grammar 1

del list[index]

Parameter
index - the position in the list where the element is to be deleted.

Return value
This function has no return value.

Grammar 2

del list[index1:index2]

Parameters
index1: index2-index1 is to delete the starting position of the element in the list, and index2 is to delete the ending position of the element in the list. Delete elements including index1 but not index2.

Return value
This function has no return value.

Grammar 3

del list

Return value
This function has no return value.

Case 1

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu']
print('初始列表为:',lis1)

print('*'*50)

del lis1[1]
print('使用del结果为:',lis1)

#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
**************************************************
使用del结果为: ['zhangsan', 'wangwu', 'zhaoliu', 'wangwu']
'''

Case 2

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu']
print('初始列表为:',lis1)

print('*'*50)

del lis1[1:3]
print('使用del结果为:',lis1)

#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
**************************************************
使用del结果为: ['zhangsan', 'zhaoliu', 'wangwu']

'''

Case 3

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu']
print('初始列表为:',lis1)

print('*'*50)

del lis1
print('使用del结果为:',lis1)

#输出结果
'''
NameError: name 'lis1' is not defined. Did you mean: 'list'?
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
**************************************************
'''

5. Find elements

Used to find the index position of an element in a list. When there are multiple identical elements, the index value is the position of the first retrieved element.

grammar

list.index(element)

Parameter
element - the element to find.

Return value
This function has a return value, which is the index position of the first retrieved element in the list.

Case

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu']
print('初始列表为:',lis1)

print('*'*50)

pos1=lis1.index('wangwu')
print('使用index结果为:',pos1)

#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
**************************************************
使用index结果为: 2
'''

Case analysis Judging
from the execution results, index will retrieve the element 'wangwu'. There are 2 'wangwu' elements in the list, and the return value is the index position when 'wangwu' is retrieved for the first time. But be aware that if the element is not in the list, the Python interpreter will report an error.

6. Other functions of list

1. The reverse function
can reverse the list, similar to "[::-1]", but the reverse function modifies the original list and has no return value.

grammar

list.reverse()

Parameters
None

Return value
This function has no return value.

Case

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu']
print('初始列表为:',lis1)

print('*'*50)

lis1.reverse()
print('使用reverse结果为:',lis1)

#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
**************************************************
使用reverse结果为: ['wangwu', 'zhaoliu', 'wangwu', 'lisi', 'zhangsan']
'''

Case analysis Judging
from the execution results, the reverse function arranges the list elements in reverse order. And the function has no return value.

2. The reversed function
sorts the given sequence (including list, tuple, string and range(n) interval) in reverse order, and this function can return an iterator of the reverse sequence (used to traverse the reverse sequence). The reversed() function performs reverse order operations and does not modify the order of elements in the original sequence.

grammar

list(reversed(list))

Parameter
list - the list to be ranked in reverse order

Return value
This function has a return value, which is a list sorted in reverse order.

Case

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu']
print('初始列表为:',lis1)

print('*'*50)

res=list(reversed(lis1))
print('使用reversed结果为:',res)

print('*'*50)

print('原来的列表为:',lis1)

#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
**************************************************
使用reversed结果为: ['wangwu', 'zhaoliu', 'wangwu', 'lisi', 'zhangsan']
**************************************************
原来的列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu']
'''

Case analysis Judging
from the execution results, the reversed function has sorted the list in reverse order and has a return value. Moreover, the reversed function does not modify the list itself.

3. The count function
is used to count the number of times a certain element appears in the list and has a return value.

grammar

list.count(element)

Parameter
element-the element to be counted.

Return value
This function returns the number of times an element appears in the list.

Case

lis1=['zhangsan','lisi','wangwu','zhaoliu','wangwu','xiaoming','yagao']
print('初始列表为:',lis1)

print('*'*50)

num1=lis1.count('wangwu')
num2=lis1.count('laowang')

print('wangwu出现的次数为:',num1)
print('laowang出现的次数为:',num2)

#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu', 'xiaoming', 'yagao']
**************************************************
wangwu出现的次数为: 2
laowang出现的次数为: 0
'''

Case analysis Judging
from the execution results, the function count counts the number of elements to be found in the list. When there is no element to be found in the list, the number is 0. And the function has a return value.

4. Sort and sorted functions
sort and sorted are both python’s own sorting functions, and their usage is basically the same.
The biggest difference is that sort will change the original list and has no return value. Sorted does not change the original list and returns a new list.

grammar

list.sort()
sorted(list)

Return value
The sort function has no return value.
The sorted function has a return value.

Case


print('*'*50)

lis2=sorted(lis1)

print('使用sorted后的原来的列表为:',lis1)
print('使用sorted后的返回的列表为:',lis2)

print('*'*50)

lis1.sort()
print('使用sort后的列表为:',lis1)


#输出结果
'''
初始列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu', 'xiaoming', 'yagao']
**************************************************
使用sorted后的原来的列表为: ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wangwu', 'xiaoming', 'yagao']
使用sorted后的返回的列表为: ['lisi', 'wangwu', 'wangwu', 'xiaoming', 'yagao', 'zhangsan', 'zhaoliu']
**************************************************
使用sort后的列表为: ['lisi', 'wangwu', 'wangwu', 'xiaoming', 'yagao', 'zhangsan', 'zhaoliu']
'''

Case analysis Judging
from the running results, both the sorted and sort functions sort the list. Among them, the sort function modifies the original list and has no return value. The sorted function does not modify the original list and has a return value.

Guess you like

Origin blog.csdn.net/weixin_44793743/article/details/126676161