Python3 Sets
A set is an unordered sequence of non-repeating elements.
Elements in a set do not repeat, and common set operations such as intersection, union, and difference can be performed.
Sets can be created using curly braces { } with elements separated by commas, or using the set() function.
Creation format:
Here is a simple example:
Note: To create an empty set, you must use set() instead of { }, because { } is used to create an empty dictionary.
More example demonstrations:
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”Similar to list comprehensions, sets also support set comprehensions:
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”Basic Set Operations
Section titled “Basic Set Operations”1. Adding Elements
Section titled “1. Adding Elements”Syntax format:
Adds element x to set s. If the element already exists, no operation is performed.
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”There is another method that can also add elements, and its parameters can be lists, tuples, dictionaries, etc. The syntax format is as follows:
x can have multiple values, separated by commas.
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”2. Removing Elements
Section titled “2. Removing Elements”Syntax format:
Removes element x from set s. If the element does not exist, an error will occur.
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”There is also another method for removing elements from a set, and it will not raise an error if the element does not exist. The format is as follows:
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”We can also randomly remove an element from the set. The syntax format is as follows:
Script Mode Example (Python 3.0+)
Section titled “Script Mode Example (Python 3.0+)”Output:
The result varies each time it is executed.
The pop method of a set will arrange the set in an unordered manner and then remove the first element from the left of this unordered arrangement.
3. Counting Set Elements
Section titled “3. Counting Set Elements”Syntax format:
Counts the number of elements in set s.
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”4. Clearing a Set
Section titled “4. Clearing a Set”Syntax format:
Clears set s.
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”5. Checking if an Element Exists in a Set
Section titled “5. Checking if an Element Exists in a Set”Syntax format:
Checks if element x is in set s. Returns True if it exists, otherwise returns False.
Example (Python 3.0+)
Section titled “Example (Python 3.0+)”Complete List of Built-in Set Methods
Section titled “Complete List of Built-in Set Methods”| Method | Description |
| add() | Adds an element to the set |
| clear() | Removes all elements from the set |
| copy() | Copies a set |
| difference() | Returns the difference of multiple sets |
| difference_update() | Removes elements from the set that also exist in the specified set. |
| discard() | Deletes a specified element from the set |
| intersection() | Returns the intersection of sets |
| intersection_update() | Returns the intersection of sets. |
| isdisjoint() | Determines whether two sets contain the same elements. Returns True if they do not, otherwise False. |
| issubset() | Determines whether the specified set is a subset of the method’s argument set. |
| issuperset() | Determines whether the method’s argument set is a subset of the specified set |
| pop() | Randomly removes an element |
| remove() | Removes the specified element |
| symmetric_difference() | Returns the set of non-repeating elements from two sets. |
| symmetric_difference_update() | Removes elements in the current set that are the same as elements in another specified set, and inserts elements from the other specified set that are different into the current set. |
| union() | Returns the union of two sets |
| update() | Adds elements to the set |
| len() | Counts the number of elements in the set |