Detailed explanation of adding and deleting collection elements in Python

Welcome to stationmaster Online Station master school study Python Knowledge, this paper studies in Python Add and remove from aggregate Element details. This knowledge point is mainly about adding and Delete element , including: adding elements to a collection in Python can use add () method implementation and use update () method Add collection element , use del Use the command to delete the entire collection and clear () method to empty collection elements and use pop () method to delete the last element and use remove () method to delete the specified element, and use the remove() method to delete multiple elements.

catalogue

1. Adding collection elements in Python

1.1. Adding elements to a collection in Python can be implemented using the add() method.

1.1.1. Use the add() method to add a single set element.

1.1.2. Use the add() method to add multiple collection elements.

1.2. Use the update() method to add collection elements

2. Deleting collection elements in Python

2.1. Delete the whole set with del command

2.2. Use the clear() method to clear the collection elements

2.3. Use pop() method to delete the last element

2.4. Use the remove() method to delete the specified element

2.5. Use the remove() method to delete multiple elements

 

Set is a variable sequence, so in Create collection After, you can also add or delete elements to it. below Webmaster Online On how to add and Delete collection elements A detailed interpretation.

1. Adding collection elements in Python

1.1. Adding elements to a collection in Python can be implemented using the add() method.

Adding elements can be divided into adding a single element and adding multiple elements.

1.1.1. Use the add() method to add a single set element.

The syntax is as follows:

setname.add(element)

Where, setname represents the collection to add elements; Element indicates the content of the element to be added. It can only be used here character string , number, boolean type True or False, etc. cannot be used list,tuple And so on.

For example, four beauties in a class have good art scores, and a new beauty has good art scores recently. It is required to create a collection, and then add a name to the collection, code As follows:

name = {'Meilin','Meng Jie','Shirley','sego '}
name.add('Mei Mei')
print(name)

The operation results are as follows:

{'Shirley', 'Mei Mei', 'Meng Jie', 'sego ', 'Meilin'}
>>> 

Can you add a single element or multiple elements? Let's try the following code:

name = {'Meilin','Meng Jie','Shirley','sego '}
name.add(['Mei Mei','Honghong','Gorgeous','Fangfang'])
print(name)

The operation result is:

Traceback (most recent call last):
  File "D:\Python\Python310\Doc\000.py", line 2, in <module>
    name.add(['Mei Mei','Honghong','Gorgeous','Fangfang'])
TypeError: unhashable type: 'list'
>>> 

The result of translating "TypeError: unhashable type: 'list'" is: TypeError: non hashable type: 'List', which means that multiple elements cannot be added directly.

But is there any way to add multiple elements using add? There are still some answers. It's just a little more trouble and more energy. Take a turn. Here's the answer:

1.1.2. Use the add() method to add multiple collection elements.

We define two sets and use the add() method to add the second set element to the first set.

For example, define three courses in the morning and three courses in the afternoon of a class as a set, and finally output the latest set. The code is as follows:

a = {'language','English','mathematics'}
b = {'Politics','history','Geography'}
for i in b:
    a.add(i)
print(a)    

The operation results are as follows:

{'language', 'mathematics', 'English', 'Politics', 'Geography', 'history'}
>>> 

1.2. Use the update() method to add collection elements

name = {'Meilin','Meng Jie','Shirley','sego '}
name.update(['Mei Mei'])
print(name)

The operation results are as follows:

{'Mei Mei', 'Meng Jie', 'Shirley', 'Meilin', 'sego '}
>>> 

The webmaster reminds you online that the update() method is used here to add collection elements, and the added elements use "[]" instead of or parentheses!

Using or not using [] will result in results that are not what we want, such as:

name = {'Meilin','Meng Jie','Shirley','sego '}
name.update('Mei Mei')
print(name)

The operation result is:

{'Shirley', 'Meng Jie', 'sego ', 'Meilin', 'plum blossom'}
>>> 

The word "Mei" is missing. It should be "Mei Mei" instead of "Mei Mei"

Similarly, it is not possible to use parentheses, such as:

name = {'Meilin','Meng Jie','Shirley','sego '}
name.update(('Mei Mei'))
print(name)

Operation results:

{'sego ', 'plum blossom', 'Shirley', 'Meilin', 'Meng Jie'}
>>> 

This is not what we want.

Of course, using the update() method, in addition to adding one element, it is also possible to add multiple elements! For example:

name = {'Meilin','Meng Jie','Shirley','sego '}
name.update(['Mei Mei','Honghong','Gorgeous','Fangfang'])
print(name)

The operation result is:

{'Meilin', 'Honghong', 'Mei Mei', 'Shirley', 'Fangfang', 'Gorgeous', 'Meng Jie', 'sego '}
>>> 

So if you add multiple elements, the update() method is simpler than the add() method.

2. Deleting collection elements in Python

After talking about adding collection elements, let's talk about deleting collection elements.

There are four methods to delete a collection element. There are four types: delete the entire collection with del command, empty the collection element with clear() method, delete the last element with pop() method, and delete the specified element with remove() method. The webmaster will explain to you one by one online:

2.1. Delete the whole set with del command

name = {'Meilin','Meng Jie','Shirley','sego '}
del name     # Delete the entire collection, no printing operation is allowed

The operation results are as follows:

>>> 

2.2. Use the clear() method to clear the collection elements

name = {'Meilin','Meng Jie','Shirley','sego '}
name.clear()     # Empty collection elements
print(name)

The operation results are as follows:

set()
>>> 

2.3. Use pop() method to delete the last element

name = {'Meilin','Meng Jie','Shirley','sego '}
name.pop()     # Delete last element
print(name)

The operation result is:

{'Shirley', 'Meilin', 'Meng Jie'}
>>> 

2.4. Use the remove() method to delete the specified element

name = {'Meilin','Meng Jie','Shirley','sego '}
name.remove('Meng Jie')     # Delete a specified element
print(name)

The operation result is:

{'Meilin', 'sego ', 'Shirley'}
>>> 

Of course, when we delete a collection element, we delete not only one element, but also multiple elements. Next, I will introduce the use of the remove() method to delete multiple elements.

Let's delete multiple elements directly and see how to achieve one effect:

a = {'language', 'mathematics', 'English', 'Politics', 'Geography', 'history'}
a.remove(['Politics', 'Geography', 'history'])
print(a)

The operation results are as follows:

Traceback (most recent call last):
  File "D:\Python\Python310\Doc\000.py", line 2, in <module>
    a.remove(['Politics', 'Geography', 'history'])
TypeError: unhashable type: 'list'
>>> 

The result of the error is the same as the above error, but I can still find it obtain The methods are formally introduced below:

2.5. Use the remove() method to delete multiple elements

Use the remove() method to delete multiple elements. We learn from the add method to add multiple elements. Set ab two sets, and delete the small set from the large set. The code is as follows:

a = {'language', 'mathematics', 'English', 'Politics', 'Geography', 'history'}
b = {'Politics','history','Geography'}
for i in b:
    a.remove(i)
print(a)

The operation results are as follows:

{'language', 'English', 'mathematics'}
>>> 

So far, this article has learned the detailed explanation of adding and deleting collection elements in Python, including: adding elements to a collection in Python can be realized by using the add() method, adding collection elements by using the update() method, deleting the entire collection by using the del command, emptying collection elements by using the clear() method, deleting the last element by using the pop() method, and using remove() Method to delete the specified element, and use the remove () method to delete multiple elements. It's all explained. Next, we will explain the detailed explanation of intersection, union and difference set operations of sets in Python.

Pay attention to [webmaster online] and let the novice Xiaobai system learn Python on a zero basis. Thank you for your attention, praise, forwarding and comments!

Tags: Python Back-end

Posted on Sun, 21 Nov 2021 13:26:01 -0500 by TPerez