1. Methods to add elements to List
1.1 Python append() method adds elements
append()
The method is used to append elements at the end of the list. The syntax format of this method is as follows:
(obj)
in,listname
Indicates a list of elements to be added;obj
Represents the data added to the end of the list, which can be a single element, a list, a tuple, etc.
1.2 Python extend() method adds elements
The difference between extend() and append() is:extend()
Instead of treating lists or ancestors as a whole, they add the elements they contain to the list one by one.
The syntax format of the extend() method is as follows:
(obj)
in,listna
me refers to the list of elements to be added;obj
Represents the data added to the end of the list, which can be a single element, a list, a tuple, etc.
Example:
l = ['Python', 'C++', 'Java'] #Add elements('C') print(l) # Add tuples, the primitives are split into multiple elements t = ('JavaScript', 'C#', 'Go') (t) print(l) # Append the list, the list is also split into multiple elements(['Ruby', 'SQL']) print(l)
Running results:
['Python', 'C++', 'Java', 'C']
['Python', 'C++', 'Java', 'C', 'JavaScript', 'C#', 'Go']
['Python', 'C++', 'Java', 'C', 'JavaScript', 'C#', 'Go', 'Ruby', 'SQL']
1.3 Python insert() method insert() method to insert elements
append()
andextend()
Methods can only insert elements at the end of the list. If you want to insert elements somewhere in the middle of the list, you can use the insert() method.
The syntax format of insert() is as follows:
(index,obj)
in,index
Indicates the index value of the specified position.insert()
Will beobj
Insert tolistname
Listindex
The position of each element. When inserting a list or a primitive, insert() will also treat them as a whole and insert into the list as an element, which is also the same asappend()
It's the same.
2. Method to delete elements from List
Deleting elements in Python list is mainly divided into the following 3 scenarios:
- Delete based on the index of the target element's location, you can use the del keyword or
pop()
method; - Delete according to the value of the element itself, and use the list (
list
Type) providedremove()
method; - Delete all elements in the list and use the list (
list
Type) providedclear()
method.
2.1 del: Delete elements based on index value
del can delete individual elements in a list in the format:
del listname[index]
in,listname
Indicates the list name,index
Indicates the index value of the element.del
You can also delete a continuous element in the middle.
The format is:
del listname[start : end]
2.2 pop(): Delete elements based on index value
Python pop()
Methods are used to delete elements at the specified index in the list.
The specific format is as follows:
(index)
where listname represents the list name and index represents the index value. If not writingindex
Parameters, by default, will delete the last element in the list, similar to the "stack" operation in the data structure.
Most programming languages provide and pop()
The corresponding method ispush()
, This method is used to add elements to the end of the list, similar to the "stack" operation in the data structure.
But Python is an exception.Python
Not provided push()
Method, because it can be used completelyappend()
Let's replacepush()
function.
2.3 remove(): Delete according to element value
In addition to the del keyword,Python
Also providedremove()
Method, this method will delete the element itself.
It should be noted thatremove()
The method will only delete the first element with the same value as the specified value, and must be guaranteed to exist, otherwise a ValueError error will be raised.
nums = [40, 36, 89, 2, 36, 100, 7] #First delete 36(36) print(nums) #Second delete 36(36) print(nums) #Delete 78(78) print(nums)
Running results:
[40, 89, 2, 36, 100, 7]
[40, 89, 2, 100, 7]
Traceback (most recent call last):
File "C:\Users\mozhiyan\Desktop\", line 9, in <module>
(78)
ValueError: (x): x not in list
The last time we deleted it, because 78 does not exist, it caused an error, so we are using itremove()
It is best to make a judgment in advance when deleting elements.
2.4 clear(): Delete all elements of the list
Python clear()
Used to delete all elements of the list, that is, to clear the list,
Please see the following code:
url = list("/python/") () print(url)
Running results:
[]
3. List list modification elements
3.1 Modify a single element
Modifying a single element is very simple, just assign values to the element directly.
Please see the following example:
nums = [40, 36, 89, 2, 36, 100, 7] nums[2] = -26 #Use positive indexnums[-3] = -66.2 #Use negative indexprint(nums)
Running results:
[40, 36, -26, 2, -66.2, 100, 7]
After using the index to obtain the list element, the value of the element is changed by assigning =.
3.2 Modify a set of elements
Python
Supports assigning values to a set of elements through slicing syntax. When performing this operation, if the step size is not specified (step parameter),Python
It is not required that the number of newly assigned elements is the same as the number of original elements; this means that this operation can either add elements to the list or delete elements for the list.
Example:
nums = [40, 36, 89, 2, 36, 100, 7] #Modify the values of elements 1 to 4 (excluding the 4th element)nums[1: 4] = [45.25, -77, -52.5] print(nums)
Running results:
[40, 45.25, -77, -52.5, 36, 100, 7]
4. List list search elements
4.1 index() method
index()
Methods are used to find the location of an element in the list (that is, the index). If the element does not exist, it will causeValueError
Error, so it is best to use before searchingcount()
Let's judge the method.
The syntax format of index() is:
(obj,start,end)
in,listname
Indicates the list name,obj
Indicates the element to be searched,start
Indicates the starting position,end
Indicates the end position.
4.2 count() method
count()
Methods are used to count the number of times an element appears in the list.
The basic syntax format is:
(obj)
in,listname
Represents the list name,obj
Indicates the element to be counted.
ifcount()
Returning 0 means that the element does not exist in the list, socount()
It can also be used to determine whether an element in the list exists.
5. Summary
Lists in Python are modifiable types, and elements of the list can be inserted and deleted during the program running. However, tuples are non-modified types. To change them, you can insert and delete elements by constructing a new tuple to replace the old tuple.
This is the end of this article about the basic operations of Python list. For more related Python list content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!