Python Edited
Python Edited
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
Ad closed by
Report this ad Why this ad?
Python Sets
❮ Previous Next ❯
Set
A set is a collection which is unordered and unindexed. In Python sets are written with curly
brackets.
Example
Create a Set:
Run example »
Note: Sets are unordered, so the items will appear in a random order.
Access Items
You cannot access items in a set by referring to an index, since sets are unordered the items has no
index.
But you can loop through the set items using a for loop, or ask if a specified value is present in a
set, by using the in keyword.
Example
Loop through the set, and print the values:
https://www.w3schools.com/python/python_sets.asp 09-06-2019
Python Sets Page 2 of 9
for x in thisset:
print(x)
Run example »
Example
Check if "banana" is present in the set:
print("banana" in thisset)
Run example »
Change Items
Once a set is created, you cannot change its items, but you can add new items.
Add Items
To add one item to a set use the add() method.
To add more than one item to a set use the update() method.
Example
Add an item to a set, using the add() method:
thisset.add("orange")
print(thisset)
Run example »
Example
Add multiple items to a set, using the update() method:
https://www.w3schools.com/python/python_sets.asp 09-06-2019
Python Sets Page 3 of 9
print(thisset)
Run example »
Example
Get the number of items in a set:
print(len(thisset))
Run example »
Remove Item
To remove an item in a set, use the remove() , or the discard() method.
Example
Remove "banana" by using the remove() method:
thisset.remove("banana")
print(thisset)
https://www.w3schools.com/python/python_sets.asp 09-06-2019
Python Sets Page 4 of 9
Run example »
Note: If the item to remove does not exist, remove() will raise an error.
Example
Remove "banana" by using the discard() method:
thisset.discard("banana")
print(thisset)
Run example »
Note: If the item to remove does not exist, discard() will NOT raise an error.
You can also use the pop() , method to remove an item, but this method will remove the last item.
Remember that sets are unordered, so you will not know what item that gets removed.
Example
Remove the last item by using the pop() method:
x = thisset.pop()
print(x)
print(thisset)
Run example »
Note: Sets are unordered, so when using the pop() method, you will not know which item
that gets removed.
Example
The clear() method empties the set:
https://www.w3schools.com/python/python_sets.asp 09-06-2019
Python Sets Page 5 of 9
thisset.clear()
print(thisset)
Run example »
Example
The del keyword will delete the set completely:
del thisset
print(thisset)
Run example »
Example
Using the set() constructor to make a set:
Run example »
Set Methods
Python has a set of built-in methods that you can use on sets.
Method Description
https://www.w3schools.com/python/python_sets.asp 09-06-2019
Python Sets Page 6 of 9
difference_update() Removes the items in this set that are also included in another,
specified set
intersection_update() Removes the items in this set that are not present in other,
specified set(s)
symmetric_difference_update inserts the symmetric differences from this set and another
()
update() Update the set with the union of this set and others
Exercise:
Check if "apple" is present in the fruits set.
Submit Answer »
https://www.w3schools.com/python/python_sets.asp 09-06-2019