Introduction to Python Programming (006) - List Operations (1): Add, Delete, and Modify List Elements

Introduction to Python Programming (006) - List Operations (1): Add, Delete, and Modify List Elements

A list consists of a series of elements arranged in a specific order and is a built-in mutable sequence in Python. All elements of the list are placed in a pair of square brackets ([]), and two adjacent elements are separated by a comma (,). You can put any type of content such as integers, strings, lists, tuples, etc. into a list. In the same list, elements can be repeated and elements can be of different types since there is no relationship between them.

1. Create a list

1. Create a list through assignment

The syntax format is as follows:

listname = [element1, element2, element3, ... , elementn]
说明:1)listname:表示列表的名称2)element1, element2, element3, ... , elementn:表示列表中的元素

For example:

list1=[2,3,5,7,11,13,17,19,23]
list2=["Zhengzhou","Xinxiang","Luoyang","Kaifeng","Anyang"]
list3=["20230214001","刘鹏","男","2003-12-3","团员"]
list4=["001","Python从入门到精通","清华大学出版社",58.2]
list5=["经济与管理学院","机电学院","计算机学院","软件学院","文法学院","艺术学院"]
print(list1)
print(list2)
print(list3)
print(list4)
print(list5)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 3, 5, 7, 11, 13, 17, 19, 23]
['Zhengzhou', 'Xinxiang', 'Luoyang', 'Kaifeng', 'Anyang']
['20230214001', '刘鹏', '男', '2003-12-3', '团员']
['001', 'Python从入门到精通', '清华大学出版社', 58.2]
['经济与管理学院', '机电学院', '计算机学院', '软件学院', '文法学院', '艺术学院']

2. Use the list() function to create a list

The syntax format is as follows:

列表名 = list()        # 创建空列表
列表名 = list(列表名)  # 使用 list() 函数将列表传递至新列表

For example:

list1=[2,3,5,7,11,13,17,19,23]
list2 = list()  # 创建空列表
list3 = list1   # list1和list3指向同一个地址,如果修改一个列表的元素值,另一个列表也被修改
list4 = list(list1)
list5 = list1[:]  # 将列表list1中的所有元素赋值给list5
print(list2)
del list1[2:6]
print(list3)
print(list4)
print(list5)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[]
[2, 3, 17, 19, 23]
[2, 3, 5, 7, 11, 13, 17, 19, 23]
[2, 3, 5, 7, 11, 13, 17, 19, 23]

3. Use the copy() method to copy the list

The syntax format is as follows:

listname = 列表名.copy()   # 将列表数据复制给新列表

For example:

list1=[2,3,5,7,11,13,17,19,23]
list2 = list1.copy()
print(list2)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 3, 5, 7, 11, 13, 17, 19, 23]

4. Merge list

The syntax format is as follows:

listname = 列表1 + 列表2   # 将两个列表数据合并至新的列表

For example:

list1 = ["张三","李四","王五","赵六"]
list2 = ["Tom","Jerry","Kate","Rose"]
list3 = list1 + list2
print(list3)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['张三', '李四', '王五', '赵六', 'Tom', 'Jerry', 'Kate', 'Rose']

Commonly used methods for list operations are shown in the following table:

method describe
append() Add an element to the end of the list
clear() Remove all elements from the list
copy() Return a copy of the list
count() Returns the number of elements with the specified value
len() Returns the length of the list
extend() Appends a list element (or any iterable) to the end of the current list
index() Returns the index of the first element with the specified value
insert() Add element at specified position
pop() Delete the element at the specified position
remove() Delete items with specified value
reverse() Reverse the order of the list
sort() Sort the list

2. Basic operations of lists

1. Index

The syntax format is as follows:

listname[index]
说明:1)index:索引编号,第1项编号为02)正向索引:从左向右,第一项的索引为0,第二项的索引为1,依次类推。3)逆向索引:从右向左,-1 表示最后一个项目,-2 表示倒数第二个项目,依此类推。

For example:

list1 = ["张三","李四","王五","赵六"]
print(list1[0])   # 第1个元素
print(list1[1])   # 第2个元素
print(list1[-1])  # 最后1个元素

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
张三
李四
赵六

2. Slice

The syntax format is as follows:

listname[start_index: end_index: step]
说明:1)在中括号中以冒号分隔索引值。2)start_index:表示要截取的开始索引值。该参数可以省略,表示从第一个字符开始。3)end_index:表示要截取的结束索引值。该参数可以省略,表示截取到最后一个字符结束。4)step:表示切片步长,该参数可以省略,默认值为 1

For example:

list1 = ["郑州市","新乡市","洛阳市","开封市","安阳市","漯河市","南阳市"]
print(list1[:])    # 打印所有元素
print(list1[::2])  # 间隔打印所有元素
print(list1[1:])   # 打印第1个元素之后的元素
print(list1[:-1])  # 打印最后1个元素之前的元素
print(list1[::-1]) # 倒序输出

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['郑州市', '新乡市', '洛阳市', '开封市', '安阳市', '漯河市', '南阳市']
['郑州市', '洛阳市', '安阳市', '南阳市']
['新乡市', '洛阳市', '开封市', '安阳市', '漯河市', '南阳市']
['郑州市', '新乡市', '洛阳市', '开封市', '安阳市', '漯河市']
['南阳市', '漯河市', '安阳市', '开封市', '洛阳市', '新乡市', '郑州市']

3. Repeat

The syntax format is as follows:

listname * n

For example:

list1 = ["郑州市","新乡市","洛阳市"]
str1 = "Hello "  # 字符串也可以看作是一个列表
str2 = "="
print(list1 * 3)
print(str1 * 3)
print(str2 * 30)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['郑州市', '新乡市', '洛阳市', '郑州市', '新乡市', '洛阳市', '郑州市', '新乡市', '洛阳市']
Hello Hello Hello 
==============================

4. Connection

The syntax format is as follows:

列表1 + 列表2

For example:

list1 = ["郑州市","新乡市","洛阳市"]
list2 = ["Washington","NewYork","Boston"]
str1 = "Henan "
str2 = "Xinxiang,"
str3 = "Henan Institute of Science and Technology."
print(list1 + list2)
print(str1 + str2 + str3)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['郑州市', '新乡市', '洛阳市', 'Washington', 'NewYork', 'Boston']
Henan Xinxiang,Henan Institute of Science and Technology.

5. Determine whether an element exists

The syntax format is as follows:

element in listname

For example:

list1 = ["郑州市","新乡市","洛阳市","开封市","安阳市","漯河市","南阳市"]
print("郑州市" in list1)
print("信阳市" in list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
True
False

6. Iteration (use for loop to traverse)

The syntax format is as follows:

for i in listname:
	print(i)       # 遍历输出每个元素

For example:

list1 = ["郑州市","新乡市","洛阳市","开封市","安阳市","漯河市","南阳市"]
for i in list1:print(i)
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
郑州市
新乡市
洛阳市
开封市
安阳市
漯河市
南阳市

7. Nesting of lists

For example:

list1 = [["郑州市","新乡市","洛阳市"],["张三","李四","王五"],[20,21,22,19]]
# 索引
print(list1[1])      # list1中的第2个元素(仍然是一个列表)
print(list1[1][1])   # list1中的第2个元素的第2个元素
# 切片
print(list1[:][1])   # list1中的第2个元素
print(list1[2][1:])  # list1中的第3个元素中的第2个元素之后的所有元素

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['张三', '李四', '王五']
李四
['张三', '李四', '王五']
[21, 22, 19]

3. Addition, deletion and modification of list

1. Change list items

(1) Change the specified list element by index number

The syntax format is as follows:

listname[index] =

For example:

list1 = ["郑州市","新乡市","洛阳市","开封市","安阳市","漯河市","南阳市"]
list1[1] = "Xinxiang City"
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['郑州市', 'Xinxiang City', '洛阳市', '开封市', '安阳市', '漯河市', '南阳市']

(2) Change list elements through slicing

listname[索引范围] =
说明:1)用后面的值替换索引范围内的列表项(列表项的数量不一定相等)2)值的形式可以有如下几种:
listname[:3] = 2,4,8
listname[:3] = [2,4,8]
listname[:3] =2,4,8

For example:

list1 = [2,4,6,8,10,12,14,16,18]
list1[1:4] = 11,22,33  
print(list1)
list1[1:3] = [11,12,13]  # 列表项的个数与切片的列表项个数不相等
print(list1)
list1[1:4] = (20,21,22,23,24,25)
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 11, 22, 33, 10, 12, 14, 16, 18]
[2, 11, 12, 13, 33, 10, 12, 14, 16, 18]
[2, 20, 21, 22, 23, 24, 25, 33, 10, 12, 14, 16, 18]

2. Add list items

(1) Use the append() method to append list elements

By default, using the append() method adds elements to the end of the list. The syntax is as follows:

listname.append()

For example:

list1 = [2,4,6,8,10,12,14,16,18]
list1.append(100)
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6, 8, 10, 12, 14, 16, 18, 100]

(2) Use the insert() method to append list elements

The insert() method can add elements to the specified position in the list. After the addition, the original position and the elements behind it are automatically moved back. The syntax format is as follows:

listname.insert(index,)

For example:

list1 = [2,4,6,8,10,12,14,16,18]
list1.insert(3,100)
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6, 100, 8, 10, 12, 14, 16, 18]

(3) Use the extend() method to append list elements

The extend() method adds all the values ​​in another list to the end of the list at once. The syntax format is as follows:

listname.extend(序列)

For example:

list1 = [2,4,6,8,10]
list2 = [11,13,15]
list3 = ['a','b','c']
list1.extend(list2)
list2.extend(['a','b','c'])
print(list1)
print(list2)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6, 8, 10, 11, 13, 15]
[11, 13, 15, 'a', 'b', 'c']

3. Delete list items

(1) Use the remove() method to delete list elements: delete based on element value

The remove() method is used to remove the specified element. When there are multiple target value elements to be deleted, only one element can be deleted in each execution, and the one with the smallest index position is deleted. The syntax format is as follows:

listname.remove(元素)

For example:

list1 = [2,4,6,8,10,12,14,16,10]
list1.remove(6)
print(list1)
list1.remove(10)  # 删除第1个元素值为10的元素
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 8, 10, 12, 14, 16, 10]
[2, 4, 8, 12, 14, 16, 10]

(2) Use the pop() method to delete list elements: delete according to index

The pop() method is used to delete the element at the specified index position. If no argument is specified, the last element is removed. The syntax format is as follows:

listname.pop()
listname.pop(index)

For example:

list1 = [2,4,6,8,10,12,14,16]
list2 = [1,3,5,7,9,11,13]
list1.pop()
print(list1)
list1.pop()
print(list1)
list1.pop()
print(list1)
list2.pop(3)
print(list2)
list2.pop(3)
print(list2)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6, 8, 10, 12, 14]
[2, 4, 6, 8, 10, 12]
[2, 4, 6, 8, 10]
[1, 3, 5, 9, 11, 13]
[1, 3, 5, 11, 13]

(3) Use the del keyword to delete a list or a specified list element: delete based on index or range

The syntax format is as follows:

del listname           # 删除整个列表(从内存中删除)
del listname[index]    # 删除指定索引位置的列表元素
del listname[索引范围]  # 删除指定索引范围内的列表元素

For example:

A) Delete the entire list:

list1 = [2,4,6,8,10,12,14,16]
del list1
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
Traceback (most recent call last):
  File "C:\Python\Python38\First.py", line 3, in <module>
    print(list1)
NameError: name 'list1' is not defined

B) Delete the list element at the specified index position

list1 = [2,4,6,8,10,12,14,16]
del list1[3]
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6, 10, 12, 14, 16]

C) Delete list elements within the specified index range

list1 = [2,4,6,8,10,12,14,16]
del list1[3:]
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6]

(4) Use clear() to delete list elements

The clear() method can clear all elements in the list (the cleared list is called an empty list). The syntax format is as follows:

listname.clear()

For example:

list1 = [2,4,6,8,10,12,14,16]
list1.clear()
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[]

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132118833