Python - method to add list elements

append() function

      The append function is used to add elements at the end of the list, and its basic usage is:

list = [1, 2, 3]
print(list)
list.append(4)
print(list)

      Print result:

[1, 2, 3]
[1, 2, 3, 4]

      Python lists support adding different types of elements, including common types (numbers, strings) and collection types (lists, tuples, etc.) as follows:

list = [1, 2, 3]
print(list)
list.append('hank')
print(list)

      Print result:

[1, 2, 3]
[1, 2, 3, 'hank']

      In addition, python lists also support list nesting, as follows:

list1 = [1, 2, 3]
print(list1)
list2 = ['hank', 'mark']
list1.append(list2)
print(list1)

      Print result:

[1, 2, 3]
[1, 2, 3, ['hank', 'mark']]

      It should be noted that when using the append() function to add a list, the reference address of the list is added, not the content of the list. When the added list is modified, the added list will also change synchronously, as follows:

list1 = [1, 2, 3]
print(list1)
list2 = ['hank', 'mark']
list1.append(list2)
print(list1)
list2.append('alice')
print(list1)

      Print result:

[1, 2, 3]
[1, 2, 3, ['hank', 'mark']]
[1, 2, 3, ['hank', 'mark', 'alice']]

      It can be seen that when list2 changes, list1 also changes synchronously. Use the id() function to check:

list1 = [1, 2, 3]
list2 = ['hank', 'mark']
list1.append(list2)
print(list2, id(list2))
print(list1, id(list1[1]))

      Print result:

['hank', 'mark'] 140607985072768
[1, 2, 3, ['hank', 'mark']] 140607894028560

      It can be seen that the address of list2 is the same as the address of list[1], this is because append(list2) adds the reference address of list2, not the content of list2. That is, the append() function makes a shallow copy, and sometimes the program needs a deep copy of the list, especially when recursion and backtracking are involved.

copy.deepcopy()

      You can use the copy.deepcopy() function to deep copy the content of the list instead of the reference address of the list, as follows:

list1 = [1, 2, 3]
list2 = ['hank', 'mark']
list1.append(copy.deepcopy(list2))
print(list2,id(list2))
print(list1, id(list1[1]))
list2.append('alice')
print(list1)

      Print result:

['hank', 'mark'] 140465798184640
[1, 2, 3, ['hank', 'mark']] 140465697685776
[1, 2, 3, ['hank', 'mark']]

      It can be seen that the address of list2 is not the same as the address of list1[1], indicating that copy.deepcopy() copies the contents of list2. In addition, it can be seen that when the content of list2 changes, it does not affect the content of list1, and 'alice' is not added to list1.

Use the slice feature list[:]

      Slicing can copy list elements to generate new list objects, as follows:

list1 = [1, 2, 3]
print(list1, id(list1))
print(list1[:], id(list1[:]))

      Print result:

[1, 2, 3] 140197983479168
[1, 2, 3] 140197983478976

      It can be seen that list1[:] generates a new list object, which is different from the memory address of list1, so it can also solve the problem of deep and shallow copying of the append function, as follows:

list1 = [1, 2, 3]
list2 = ['hank', 'mark']
list1.append(list2[:])
print(list1)
list2.append('alice')
print(list1)

      Print result:

[1, 2, 3, ['hank', 'mark']]
[1, 2, 3, ['hank', 'mark']]

      It can be seen that when list2 changes, list1 added to list2 does not change. This is because list1 actually adds list2[:], which is a new list object with the same content as list2, not a reference to list2. address.


extend() function

      The extend() function also adds elements at the end of the list, but its parameters can only be lists, and the added list elements are copied into the list that calls the extend function, which can be simply understood as merging the two lists, as follows:

list1 = [1, 2, 3]
print(list1)
list2 = ['hank', 'mark']
list1.extend(list2)
print(list1)

      Print result:

[1, 2, 3]
[1, 2, 3, 'hank', 'mark']

      The extend function copies the content of the element, and there is no deep and shallow copy problem, as follows:

list1 = [1, 2, 3]
print(list1)
list2 = ['hank', 'mark']
list1.extend(list2)
print(list1)
list2.append('alice')
print(list1)

      Print result:

[1, 2, 3]
[1, 2, 3, 'hank', 'mark']
[1, 2, 3, 'hank', 'mark']

      It can be seen that when list2 changes, list1 does not change.


+ operator

      The + operator adds (splices) two lists and returns a new list object, which consumes additional memory, as follows:

list1 = [1, 2, 3]
list2 = ['hank', 'mark']
print(list1, id(list1))
print(list2, id(list2))
print(list1 + list2, id(list1 + list2))

      Print result:

[1, 2, 3] 140222989918720
['hank', 'mark'] 140222989925696
[1, 2, 3, 'hank', 'mark'] 140222989916160

      It can be seen that + causes list1 and list2 to be spliced, and a new list object is generated, occupying a new memory address.

list1 = [1, 2, 3]
print(list1, id(list1))
list2 = ['hank', 'mark']
list1 = list1 + list2
print(list1, id(list1))

      Print result:

[1, 2, 3] 140420138983616
[1, 2, 3, 'hank', 'mark'] 140420138983424

      It can be seen that the address of list1 has changed and a new memory address is used.

Guess you like

Origin blog.csdn.net/fzz97_/article/details/128888486