Python Sets
- Previous Page Python Tuples
- Next Page Python Dictionaries
Set (Set)
The set is an unordered and unindexed collection. In Python, sets are written using curly braces.
an instance
Create set:
thisset = {"apple", "banana", "cherry"} print(thisset)
Note:The set is unordered, so you cannot determine the display order of the items.
Access item
You cannot access the items in the set by reference index because the set is unordered and items do not have an index.
But you can use for
Loop through set items, or use in
Keyword query to check if a specified value exists in the set.
an instance
Traverse the set and print the values:
thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)
an instance
Check if "banana" exists in the set:
thisset = {"apple", "banana", "cherry"} print("banana" in thisset)
Change item
Once the set is created, you cannot change the items, but you can add new items.
Add item
To add an item to the set, please use add()
Method.
To add multiple items to the set, please use update()
Method.
an instance
Using add()
Method to add an item to the set:
thisset = {"apple", "banana", "cherry"} thisset.add("orange") print(thisset)
an instance
Using update()
Method to add multiple items to the set:
thisset = {"apple", "banana", "cherry"} thisset.update(["orange", "mango", "grapes"]) print(thisset)
Get the length of Set
To determine how many items are in the set, please use len()
Method.
an instance
To get the number of items in the set:
thisset = {"apple", "banana", "cherry"} print(len(thisset))
Delete item
To delete an item from the set, please use remove()
Or discard()
Method.
an instance
Using remove()
Method to delete "banana":
thisset = {"apple", "banana", "cherry"} thisset.remove("banana") print(thisset)
Note:If the item to be deleted does not exist, then remove()
It will cause an error.
an instance
Using discard()
Method to delete "banana":
thisset = {"apple", "banana", "cherry"} thisset.discard("banana") print(thisset)
Note:If the item to be deleted does not exist, then discard()
It will not cause an error.
You can also use pop()
Method to delete an item, but this method will delete the last item. Remember, the set is unordered, so you will not know which item is being deleted.
pop()
The return value of the method is the item that was deleted.
an instance
Using pop()
Method to delete the last item:
thisset = {"apple", "banana", "cherry"} x = thisset.pop() print(x) print(thisset)
Note:The set is unordered, so when using pop()
When the method is executed, you will not know which item is deleted.
an instance
clear()
Method to clear the set:
thisset = {"apple", "banana", "cherry"} thisset.clear() print(thisset)
an instance
del
Completely delete the set:
thisset = {"apple", "banana", "cherry"} del thisset print(thisset)
Merge two sets
In Python, there are several methods to combine two or more sets.
You can use the union() method to return a new set containing all items from both sets, or you can use the update() method to insert all items from one set into another set:
an instance
The union() method returns a new set that contains all items from both sets:
set1 = {"a", "b", "c"} set2 = {1, 2, 3} set3 = set1.union(set2) print(set3)
an instance
The update() method inserts items from set2 into set1:
set1 = {"a", "b", "c"} set2 = {1, 2, 3} set1.update(set2) print(set1)
Note:union() and update() both exclude any duplicates.
There are other methods to combine two sets and retain or never retain duplicates, please see the complete list of set methods at the bottom of this page.
the set() constructor
You can also use set()
Constructor to create a set.
an instance
Using set()
Constructor to create a set:
thisset = set(("apple", "banana", "cherry")) # Please note the double parentheses print(thisset)
Set Methods
Python has a set of built-in methods that can be used on sets (set).
Method | Description |
---|---|
add() | Add an element to the set. |
clear() | Delete all elements from the set. |
copy() | Return a copy of the set. |
difference() | Return a set that contains the differences between two or more sets. |
difference_update() | Delete items from this set that are also included in another specified set. |
discard() | Delete a specified item. |
intersection() | Return a set that is the intersection of two other sets. |
intersection_update() | Delete items from this set that are not present in the other specified sets. |
isdisjoint() | Return whether two sets have an intersection. |
issubset() | Return whether another set contains this set. |
issuperset() | Return whether this set contains another set. |
pop() | Remove an element from the set. |
remove() | Delete a specified element. |
symmetric_difference() | Return a set containing the symmetric difference of two sets. |
symmetric_difference_update() | Insert the symmetric difference of this set and another set. |
union() | Return a set containing the union of sets. |
update() | Update the set with the union of this set and other sets. |
- Previous Page Python Tuples
- Next Page Python Dictionaries