Specific examples of Python list data addition and deletion

Time:2021-11-23

1. Use the append function to add data to the list. By default, the data is appended to the end.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author: ape programming
@Blog (personal blog address): www.codersrc.com
@File: Python list.py
@Time:2021/3/22 00:37
@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
 
"""
 
list1 = list() #Define an empty list
print("list1 : ",list1)
list1.append("hello") #Add the string 'hello' to the end of the list
list1.append(True)    #Adds a Boolean value true to the end of the list
print("list1 : ",list1)
 
'''
Output results:
 
list1 :  []
list1 :  ['hello', True]
 
'''

2. The data in the list is from left to right, and the index value is incremented by 0 by default. Similar to the index value of the string, the Del keyword is used for deletion. When the list is directly deleted, it is directly deleted according to the index value corresponding to the data.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author: ape programming
@Blog (personal blog address): www.codersrc.com
@File: Python list.py
@Time:2021/3/22 00:37
@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
 
"""
 
list2 = [1,2,3,4,5,6,7,False,"python"]
print(Before deleting data: {}.format(list2))
del list2[0#Delete the first data in the list (the index value is equal to 0). At this time, the data in List2 is [2,3,4,5,6,7, false, "Python"]
print("After first data: {}".format(list2))
del list2[0#Based on the last result, delete (the index value is equal to 0) the first data. At this time, the data in List2 is [3,4,5,6,7, false, "Python"]
print("After second data: {}".format(list2))
del list2[3#Based on the last result, delete (the index value is equal to 3) the fourth data. At this time, the data in List2 is [3,4,5,7, false, "Python"]
print("After the third data: {}".format(list2))
 
 
'''
Output results:
 
Before deleting data: [1, 2, 3, 4, 5, 6, 7, false, 'Python']
After the first data: [2, 3, 4, 5, 6, 7, false, 'Python']
After the second data: [3, 4, 5, 6, 7, false, 'Python']
After the third data: [3, 4, 5, 7, false, 'Python']
 
 
'''

Instance extension:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#1     Generate list
my_list1=[1,2,3,4,5]
print(my_list1)
print(type(my_list1))
 
my_list2=(6,7,8,9,12)
my_list3=list(my_list2)  #Use the list function to create a list
print(my_list2)
print(my_list3)
 
 
#2      Accessing elements in a list
print(my_list3[2])
 
 
#3       Delete list
del my_list3
 
 
#4       Adding elements to a table
mylist=list(range(1,10))
mylist.append(12)      #The append () method appends an element to the end of the list
print(mylist)
 
mylist.extend("hello world")      #The appended list or tuple is treated as a whole, but only the elements in the list are appended
print(mylist)
 
 
mylist.insert(5,20)   #Add elements in the middle of the list
print(mylist)
 
 
 
#5      Delete elements in table
#Delete elements with list index
myList=list(range(1,12))
print(myList)
del myList[4]
print(mylist)
 
 
#Use the remove () function to perform the delete operation according to the value of the element itself
myList.remove(6)
print(myList)
 
#Use the clear function to delete all elements
myList.clear()
print(myList)
 
 
#       Modifying elements in a table
MyList=[1,2,3,4]
MyList[2]=100
print(MyList)

This is the end of this article about the specific examples of Python list data addition and deletion. For more information about how to add and delete Python list data, please search the previous articles of developer or continue to browse the relevant articles below. I hope you will support developer in the future!