How to Use the Python Set
In this tutorial, you'll learn what a set is in Python and how to use it. You'll learn how to create, copy, add, and remove elements from and to a set. And, you also learn how to count the number of items in the set.
What is a Set in Python?
In Python, a set is an unordered collection of unique elements, which cannot contain duplicate values.
Creating a Set in Python
A set created by surrounding objects with { } and separating the items with commas (,).
Here is an example:
empty_set = { }
vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot"}
The first line created an empty set, and the second line created a set of 7 elements.
Accessing Items
Since sets are unordered and unindexed, you can not access an individual item.
Adding Items
You can add a single item or more than one item at a time.
Add a Single Item
To add an item to a set, you use the add() method as follows:
set.add(item)
Let's take a look at an example:
vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot"}
vegetables.add("Cauliflower")
print(vegetables)
Here, we've created a set named vegetables and then added element Cauliflower to it.
Output:
{'Lettuce', 'Onion', 'Garlic', 'Asparagus', 'Cauliflower', 'Cabbage', 'Carrot', 'Broccoli'}
Add Multiple Items
If you want to add more than one items at a time, you can use the update() method as follows:
set.update(another_set)
Here is an example:
vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot"}
vegetables.update(["Cauliflower", "Okra", "Spinach", "Celery"])
print(vegetables)
Output:
{'Cauliflower', 'Carrot', 'Lettuce', 'Okra', 'Asparagus', 'Garlic', 'Celery', 'Broccoli', 'Spinach', 'Cabbage', 'Onion'}
Add an Item Already Exists
As mentioned above, since sets cannot contain duplicate values, if you add the duplicate element, nothing changes.
Here is an example:
vegetables = {"Cabbage", "Lettuce", "Garlic"}
vegetables.add("Cabbage")
print(vegetables)
Output:
{'Garlic', 'Lettuce', 'Cabbage'}
As you can see, nothing changes in the set.
Removing Items
To remove an item from a set, you can use the remove() method, or the discard() method.
Here is an example of using the remove() method:
vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot", "Cauliflower"}
vegetables.remove("Cauliflower")
vegetables.remove("Lettuce")
print(vegetables)
Result:
{'Cabbage', 'Carrot', 'Broccoli', 'Garlic', 'Asparagus', 'Onion'}
Deleting a Set
To delete a set, you can use the del keyword as follows:
del set
Here is an example:
students = {"Student A", "Student B", "Student C"}
del students
print (students) #this causes an error because students is no longer available!
Output:
NameError: name 'students' is not defined
Checking If an Item Exists
You can use the in keyword and IF statement to check if an item exists in a set.
Here is an example:
vegetables = {"Cabbage", "Lettuce", "Carrot", "Cauliflower", "Garlic", "Onion", "Asparagus", "Broccoli"}
if "Carrot" in vegetables:
print ("The item \"Carrot\" already exists!")
if "Chili Pepper" not in vegetables:
print ("The item \"Chili Pepper\" does not exist!")
Output:
The item "Carrot" already exists!
The item "Chili Pepper" does not exist!
If you want to check if an item doesn't exist in a set, you can use the in keyword and IF statement.
Iterating a Set
You can use the FOR loop statement to iterate over a set as illustrated in the following example:
students = {"Student A", "Student B", "Student C", "Student D", "Student E"}
for student in students:
print (student)
Result:
Student A
Student C
Student E
Student B
Student D
Length of a Set
To get the number of elements in a given set, you can use the len() function.
len(set)
Here is an example:
students = {"Student A", "Student B", "Student C", "Student D", "Student E"}
print("The number of items in a set is", len(students))
Output:
The number of items in a set is 5
Copying A Set in Python
To copy a set in Python, you use the set copy() method as follows:
new_set = old_set.copy()
Here is an example:
students = {"Student A", "Student B", "Student C", "Student D", "Student E"}
students_1 = students
students_2 = students.copy();
students.add("Student F")
print ("students_1:", students_1)
print ("students_2:", students_2)
Output:
students_1: {'Student E', 'Student F', 'Student C', 'Student D', 'Student A', 'Student B'}
students_2: {'Student E', 'Student D', 'Student A', 'Student B', 'Student C'}
In this tutorial, you've learned what a set is in Python and how to use it. You've learned to create, copy, add, and remove elements from and to a set. And, you also learn how to count the number of items in the tuple.