In actual development, you often need to update Python lists, including adding elements to the list, modifying elements in the table, and deleting elements. This section first learns how to add elements to the list.
The "Python Sequence" section tells us that you can use the + operator to concatenate multiple sequences; lists are a type of sequence, so you can also use + to concatenate a list.
Example:
name = ["Jason", "Alice", "Mark"] birthday = [1993, 1988, 2000] info = name + birthday print("name =", name) print("birthday =", birthday) print("info =", info)
The output is:
name = ['Jason', 'Alice', 'Mark']
birthday = [1993, 1988, 2000]
info = ['Jason', 'Alice', 'Mark', 1993, 1988, 2000]
It can be found from the running results that using + will generate a new list, and the original list will not be changed.
+ It is more used to concatenate lists, and the execution efficiency is not high. If you want to insert elements in the list, you should use the following special methods.
The append () method is used to append elements to the end of the list. The syntax of this method is as follows:
listname.append (obj)
Among them, listname represents 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:
name = ["Jason", "Alice", "Mark"] #Append element name.append ("Nicole") print (name) #Append tuple, the entire tuple is treated as an element fruit = ("Pineapple", "Mango") name.append (fruit) print (name) #Append list, the entire list is also treated as an element name.append (['Iven', 'Peggy']) print (name)
The output is:
['Jason', 'Alice', 'Mark', 'Nicole']
['Jason', 'Alice', 'Mark', 'Nicole', ('Pineapple', 'Mango')]
['Jason', 'Alice', 'Mark', 'Nicole', ('Pineapple', 'Mango'), ['Iven', 'Peggy']]
As you can see, when passing a list or tuple to the append method, this method treats them as a whole and adds them to the list as an element, forming a new list containing lists and tuples.
The difference between extend and append is that extend method does not treat lists or primitives but adds the elements they contain to the list one by one. The syntax of the extend () method is as follows:
listname.extend (obj)
Among them, listname refers to the list of elements to be added; obj represents the data added to the end of the list. It can be a single element, a list, a tuple, etc.
Example:
name = ["Jason", "Alice", "Mark"] #Append element name.extend ('Nicole') print (name) #Append tuple, the ancestor was split into multiple elements fruit = ('Pineapple', 'Mango') name. extend (fruit) print (name) #Append list, list is also split into multiple elements name. extend (['Iven', 'Peggy']) print (name)
The output is:
['Jason', 'Alice', 'Mark', 'N', 'i', 'c', 'o', 'l', 'e']
['Jason', 'Alice', 'Mark', 'N', 'i', 'c', 'o', 'l', 'e', 'Pineapple', 'Mango']
['Jason', 'Alice', 'Mark', 'N', 'i', 'c', 'o', 'l', 'e', 'Pineapple', 'Mango', 'Iven', 'Peggy']
The append and extend 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 of insert method is as follows:
listname.insert (index, obj)
Among them, index represents the index value of the specified position. Insert method inserts obj at the index element of the listname list.
When inserting a list or ancestor, insert method also treats them as a whole and inserts them into the list as an element, which is the same as append ().
Example:
name = ["Jason", "Alice", "Mark"] #INSERT ELEMENT name.insert(1, "Jay") print (name) #Inserting tuples, the entire ancestor is treated as an element fruit = ('Pineapple', 'Mango') name.insert (2, "Apple") print (name) #Insert list, the entire list is treated as one element name. insert (8, ['Iven', 'Peggy']) print (name) #Insert string, the entire string is treated as one element name.insert (1, "www.freelearningpoints.com") print (name)
The output is:
['Jason', 'Jay', 'Alice', 'Mark']
['Jason', 'Jay', 'Apple', 'Alice', 'Mark']
['Jason', 'Jay', 'Apple', 'Alice', 'Mark', ['Iven', 'Peggy']]
['Jason', 'www.freelearningpoints.com', 'Jay', 'Apple', 'Alice', 'Mark', ['Iven', 'Peggy']]
Hint, insert method is mainly used to insert elements in the middle of the list. If you only want to append elements to the end of the list, I recommend using append and extend method.
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises