Table of Contents
Python sets are a data structure in Python that are not indexed or ordered, and are written with curly brackets ( { } ). Python sets offer a major advantage of being a highly optimized method for validating whether an element is within in the set. The hash table is the base for the data structure.
Python Sets Methods
Python Set Creation
Creating a list in Python is easy by using curly brackets and surrounding the values with quotes if needed.
Input:
car_models = {"Mustang", "Camaro", "Civic", "Jetta"} print(car_models)
Output:
{'Jetta', 'Camaro', 'Mustang', 'Civic'}
Python Set Access
Python does not allow access to items in a set by referring to an index. The reason is because sets are not ordered and there’s no index. However, one can use a for loop to access items by name, which will be discussed in this tutorial later.
Python Set Looping
As stated with Python sets access, in order to locate a item, a for loop can print out each item that is in the set.
Input:
for x in car_models: print(f"Car model: {x} ")
Output:
Car model: Mustang Car model: Camaro Car model: Civic Car model: Jetta
Python Sets Validating Item
Python set item validating will determine if an item is in the list by using the keyword, in.
if…else Example 1
Input:
if "Gallardo" in car_models: print("Yes, Gallardo is in the list.") else: print("No, Gallardo is not in the list.")
Output:
No, Gallardo is not in the list.
if…else Example 2
Input:
if "Mustang" in car_models: print("Yes, Mustang is in the list.") else: print("No, Mustang is not in the list.")
Output:
Yes, Mustang is in the list.
Python Set Length
The internal method, len() will determine the length (item count) of a set.
Input:
print(len(car_models))
Output:
4
Python Adding Set Item
The internal methods, add() will add a specified item to the end of a set.
Input:
car_models.add("Gallardo") print(f"Gallardo is added to the car model set: {car_models} ")
Output:
Gallardo is added to the car model set: {'Camaro', 'Jetta', 'Gallardo', 'Mustang', 'Civic'}
Python Update Set Item
The internal method, update() will add a item to the index number specified.
Input:
car_models.update(["Mustang", "Camaro", "Civic", "Jetta", "M3"]) print(f"M3 is added to the car model set: {car_models} ")
Output:
M3 is added to the car model set: {'M3', 'Camaro', 'Jetta', 'Mustang', 'Civic', 'Gallardo'}
Python Set Item Removal
There are two methods to remove an item from a set . Below are the two methods:
- remove()
- discard()
remove()
The method remove() will remove the specified item in a string.
Input:
car_models.remove("Civic") print(f"Civic is removed from the set: {car_models} ")
Output:
Civic is removed from the set: {'M3', 'Gallardo', 'Mustang', 'Jetta', 'Camaro'}
discard()
The method discard() will remove the specified item in a string.
Input:
car_models.discard("Mustang") print(f"Mustang is discarded from the set: {car_models} ")
Output:
Mustang is discarded from the set: {'M3', 'Gallardo', 'Jetta', 'Camaro'}
Python set() Constructor
The set() constructor method to create a new set.
Input:
new_car_model_set = set(("Mustang", "Camaro", "Civic", "Jetta", "M3", "Lamborghini", "California", "Enzo")) print(f"New car model set using the set() constructor method: {new_car_model_set} ")
Output:
New car model set using the set() constructor method: {'Enzo', 'California', 'M3', 'Jetta', 'Camaro', 'Mustang', 'Lamborghini', 'Civic'}