Basic usage of () function
The append() function can add any type of element at the end of the list.
1.1 Add integers, floating point numbers, and strings
Example:
list1 = [1, 2] (0) #Add integer typeprint('list1=', list1) list2 = [1, 2] (1.23) #Add floating point number typeprint('list2=', list2) list3 = [1, 2] ('everything') #Add string typeprint('list3=', list3)
Output:
list1= [1, 2, 0]
list2= [1, 2, 1.23]
list3= [1, 2, 'everything']
1.2 Add a list and dictionary
Example:
list1 = [1, 2] a = [1, 2, 3] #List(a) print('list1=', list1) list2 = [1, 2] b = {'Xiao Ming': 1, 'Little Tiger': 2} #dictionary(b) print('list2=', list2)
Output:
list1= [1, 2, [1, 2, 3]]
list2= [1, 2, {'Xiao Ming': 1, 'Xiao Hu': 2}]
2. Synchronous changes in the addition list
2.1 Synchronous changes of list and dictionary
Example: List Change
name_list = ['sam', 'dean'] number_list = [1, 2, 3] name_list.append(number_list) print(number_list) print(name_list) a = 4 number_list.append(a) print(number_list) print(name_list)
Output:
[1, 2, 3]
['sam', 'dean', [1, 2, 3]]
[1, 2, 3, 4]
['sam', 'dean', [1, 2, 3, 4]] #The number_list synchronous changes in the result
2.2 Operation principle
Why does the list or dictionary in the result change when the list or dictionary that has been added also change synchronously? This is because the append() function works by calling the address of the element directly.
Example 1: List Changes
name_list = ['sam', 'dean'] number_list = [1, 2, 3] name_list.append(number_list) print(id(number_list)) #number_list reference addressprint(id(name_list[2])) #Added reference address
Output:
2926810333248
2926810333248
Example 2: Dictionary Changes
name_list = ['sam', 'dean'] number_list = {'a': 1, 'b': 2, } name_list.append(number_list) print(name_list) print(id(name_list[2])) print(id(number_list)) number_list['a'] = 3 print(number_list) print(name_list)
Output:
['sam', 'dean', {'a': 1, 'b': 2}]
1986756347904
1986756347904
{'a': 3, 'b': 2}
['sam', 'dean', {'a': 3, 'b': 2}]
It can be seen that the reference address of number_list after addition is the same as before, which means that if the original value of number_list changes, the number_list value added in name_list changes synchronously, which may cause problems in subsequent work.
2.3 Solution
We can change the function of the append() function to the reference content, which requires the copy() function.
Here we will mention two concepts of copying:
1. Shallow copy: the reference address of the copy object
2. Deep copy: copy the content of the object
The copy() function can directly copy the content of the list, so as to ensure that the added list value does not change.
Example 1: List
import copy name_list = ['sam', 'dean'] number_list = [1, 2, 3] name_list.append((number_list)) #Directly copy the content of number_listprint(number_list) print(name_list) print(id(number_list)) print(id(name_list[2])) #View the address that is added to the number_list in name_list at this time a = 4 number_list.append(a) print(number_list) print(name_list)
Output:
[1, 2, 3]
['sam', 'dean', [1, 2, 3]]
2182206779456
2182206779392
[1, 2, 3, 4]
['sam', 'dean', [1, 2, 3]]
Example 2: Dictionary
import copy name_list = ['sam', 'dean'] number_list = {'a': 1, 'b': 2, } name_list.append((number_list)) print(name_list) print(id(name_list[2])) print(id(number_list)) number_list['a'] = 3 print(number_list) print(name_list)
Output:
['sam', 'dean', {'a': 1, 'b': 2}]
2607266300864
2607266269184
{'a': 3, 'b': 2}
['sam', 'dean', {'a': 1, 'b': 2}]
It can be seen that the address of the list dictionary after deep copy has changed and the list value does not change synchronously with the original list.
3. Basic usage of extend() function
The extend() function also adds elements, but the difference from the append() function is that when adding a list or dictionary, the extend() function directly adds elements in the list or dictionary, while the append() function directly adds the list or dictionary as an element.
Example:
list1 = [1, 2] a = [1, 2, 3] (a) print(list1) list2 = ['sam', 'dean'] b = {'dd': 1, 'mm': 2} (b) print(list2)
Output:
[1, 2, 1, 2, 3]
['sam', 'dean', 'dd', 'mm'] #Element values in the dictionary are not displayed
It should be noted that when using attend() to add elements in the dictionary, the value of the element is not displayed.
Summarize
This is the article about the usage and differences between the built-in functions append() and extend() in Python. For more information about the usage of built-in functions append() and extend() in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!