removing
(index)
Deletes the element at the specified index in the list, defaults to the last element in the list, and returns the deleted value.
list1 = [1, 2, 3, 5, 8, '3'] print((3)) print(list1) print(()) print(list1) print((-1)) print(list1)
5
[1, 2, 3, 8, '3']3
[1, 2, 3, 8]8
[1, 2, 3]
(item)
Deletes an element based on its value; it will only delete the first element with the same value as the specified value, and does not return the deleted value.
Notes:The value of this element must be guaranteed to exist in the list or a ValueError error will be raised.
list2 = [1, 3, 3, 5, '3'] print((3)) print(list2) (9)
None
[1, 3, 5, '3']
Traceback (most recent call last):
File "C:/Users/chenh/PycharmProjects/pythonProject/", line 4, in <module>
(9)
ValueError: (x): x not in list
list[index]
Deletes not only the entire list, but also certain elements of the list, without returning the deleted value.
Notes:print(del list[index]) will report an error
list3 = ['Python', 'C', 'Go', 'C++', 'Java', 'VB', 'JavaScript', 'PHP'] del list3[2] print(list3) del list3[-2] print(list3) del list3[2:4] print(list3) (["SQL", "C#", "Swift"]) print(list3) del list3[-5:-2] print(list3)
['Python', 'C', 'C++', 'Java', 'VB', 'JavaScript', 'PHP']
['Python', 'C', 'C++', 'Java', 'VB', 'PHP']
['Python', 'C', 'VB', 'PHP']
['Python', 'C', 'VB', 'PHP', 'SQL', 'C#', 'Swift']
['Python', 'C', 'C#', 'Swift']
When deleting an intermediate segment, the format is del[start:end].
Where start is the start index and end is the end index. del removes the elements between start and end, excluding the elements at end.
()
Deletes all elements of the list without returning the deleted value.
list4 = [1, 2, 3, 5, 8, '3'] print(()) print(list4)
None
[]
increase
(obj)
Add to the end of a list, which can be an element, list, or tuple.
list1 = ['C', 'Python', 'Java'] ('Go') print(list1) t = ('Javascript', 'Python', 'C#') (t) print(list1) (['VB', 'PHP']) print(list1)
['C', 'Python', 'Java', 'Go']
['C', 'Python', 'Java', 'Go', ('Javascript', 'Python', 'C#')]
['C', 'Python', 'Java', 'Go', ('Javascript', 'Python', 'C#'), ['VB', 'PHP']]
(obj)
will not consider the list or tuple as a whole, but rather the elements they containone by oneAdd to list.
Note: Cannot addan odd onedigital (electronics etc)
list2 = ['C', 'Python', 'Java'] ('Go') print(list2) t = ('Javascript', 'Python', 'C#') (t) print(list2) (['VB', 'PHP']) print(list2)
['C', 'Python', 'Java', 'G', 'o']
['C', 'Python', 'Java', 'G', 'o', 'Javascript', 'Python', 'C#']
['C', 'Python', 'Java', 'G', 'o', 'Javascript', 'Python', 'C#', 'VB', 'PHP']
(index, obj)
Insert obj at position index.
When inserting a list or tuple, insert() also treats it as a whole.
list3 = ['C', 'Python', 'Java'] (1, 'C++') print(list3) t = ('Javascript', 'Python', 'C#') (2, t) print(list3) (2, ['VB', 'PHP']) print(list3)
['C', 'C++', 'Python', 'Java']
['C', 'C++', ('Javascript', 'Python', 'C#'), 'Python', 'Java']
['C', 'C++', ['VB', 'PHP'], ('Javascript', 'Python', 'C#'), 'Python', 'Java']
summarize
That's all for this post, I hope it was helpful and I hope you'll check back for more from me!