Python Sets

Understanding Python Sets
Python, a versatile and powerful programming language, offers a wide range of data structures that make it a favourite among developers. Set is a fundamental data structure used for storing collections of unique elements. In this blog post, we will learn deeper into Python Sets, exploring their definition, operations, and usage with various code examples.
What is a Python Set?
A set is an unordered collection of unique elements in Python. Unlike other data structures like lists or tuples, a set does not allow duplicate values. Sets are highly useful when you need to store distinct items or perform set-related operations such as unions and intersections.
Creating a Set
You can create a set using curly braces {} or by using the set() constructor. The objects are placed inside those brackets and are separated by comma(,).
# Using curly braces my_set = {1, 2, 3} print(my_set) # Using set() constructor my_sets = set(["Laptop", "Mobile", "Watch"]) print(my_sets)
Accessing Items in a Set
Sets are unordered collections, meaning they do not have indices like lists or tuples. Therefore, you cannot access set items by their index. However, you can use for loop to access all its items one-by-one.
my_sets = set(["Laptop", "Mobile", "Watch"]) for items in my_sets: print(items) #Output (Mobile Watch Laptop)
Adding Items to a Set
You can add items to a set using the add() or update() method. The add() method adds one item to a set.
my_sets = {"Laptop", "Mobile", "Watch"} my_sets.add("SmartWatch") print(my_sets)
Changing Items in a Set
Sets are unordered and immutable, meaning you cannot change individual items in a set. To update a set, you can remove the old item and add the new one. To remove an item from a set, use the remove() method. You should specify the value of the item you want to remove.
my_sets = {"Laptop", "Mobile", "Watch"} #Removing "Watch" my_sets.remove("Watch") #Adding "SmartWatch" my_sets.add("SmartWatch") print(my_sets)
Getting the length of Set
To get the length of the number of items in a set, use the len() method.
my_set = {1, 2, 3, 4, 5} length = len(my_set) print(length)
Checking if an item Exists
You can check if an item exists in a set using the “in” keyword. It returns a Boolean value, True if the item is in the set, and False otherwise.
my_sets = {"Laptop", "Mobile", "Watch"} print("Mobile" in my_sets) #True print("SmartWatch" in my_sets) #False
Combining Two Sets
Python sets support various set operations like union, intersection, and difference. To combine two sets into one, you can use the union() or update() method. update() method exclude duplicate items.
set = {"Laptop", "Mobile", "Watch"} set1 = {"SmartWatch", "Ipad"} combine = set.union(set1) print(combine)
Combining Two sets using update() method.
set = {"Laptop", "Mobile", "Watch"} set1 = {"SmartWatch", "Ipad"} set.update(set1) print(set)
These are some of the most common operations when working with sets in Python. Sets offer a convenient way to work with unique collections of data, making them a valuable tool in many programming scenarios.
Conclusion;
In summary, Python sets are a versatile data structure for working with collections of unique elements. They are useful for various tasks like removing duplicates, set operations, and membership testing. By understanding how to create, manipulate, and utilize sets, you can write more efficient and concise code in your Python projects. I hope you have understood this tutorial (Python Sets), but if you have any doubt don’t hesitate to ask.
Learn More;
0 Comments