Python Sets

Published by StudyMuch on

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.

Python Sets

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}

# Using set() constructor
my_sets = set(["Laptop", "Mobile", "Watch"])

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"}

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"

#Adding "SmartWatch"

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)

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)

Combining Two sets using update() method.

set = {"Laptop", "Mobile", "Watch"}
set1 = {"SmartWatch", "Ipad"}


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.


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;

13 Comments · January 23, 2024 at 3:23 pm

I?ve recently started a site, the info you offer on this web site has helped me greatly. Thank you for all of your time & work.

Casinosite · February 5, 2024 at 5:05 pm

Nice post. I used to be checking constantly this weblog and I’m impressed! Extremely helpful information specially the ultimate phase 🙂 I handle such info much. I was looking for this certain info for a very lengthy time. Thank you and good luck.

situs bokep · April 9, 2024 at 1:03 pm

Holy cow! I’m in awe of the author’s writing skills and capability to convey complicated concepts in a concise and concise manner. This article is a true gem that earns all the praise it can get. Thank you so much, author, for offering your wisdom and offering us with such a precious treasure. I’m truly appreciative!

bokep jepang · April 9, 2024 at 2:44 pm

Thanks for the several tips shared on this website. I have observed that many insurance providers offer consumers generous savings if they prefer to insure many cars with them. A significant amount of households currently have several vehicles these days, especially those with elderly teenage youngsters still dwelling at home, plus the savings for policies may soon begin. So it pays to look for a good deal.

situs porno · April 30, 2024 at 9:29 am

Thanks for giving your ideas. I’d personally also like to convey that video games have been at any time evolving. Modern technology and innovations have helped create practical and interactive games. These entertainment video games were not really sensible when the real concept was first of all being tried. Just like other forms of electronics, video games also have had to develop by way of many ages. This is testimony for the fast continuing development of video games. · May 5, 2024 at 9:23 pm

It is best to take part in a contest for among the finest blogs on the web. I’ll advocate this web site!

tlovertonet · May 6, 2024 at 2:04 am

Just what I was looking for, regards for posting.

kontol kuda · May 6, 2024 at 8:29 am

The other day, while I was at work, my sister stole my iPad and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!

bokep jepang · May 6, 2024 at 5:42 pm

I think other site proprietors should take this website as an model, very clean and great user friendly style and design, let alone the content. You’re an expert in this topic!

link bokep · May 6, 2024 at 8:37 pm

This design is incredible! You obviously know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

bokep indo · May 7, 2024 at 5:57 pm

What?s Happening i’m new to this, I stumbled upon this I’ve found It absolutely helpful and it has helped me out loads. I hope to contribute & assist other users like its helped me. Good job.

video porno · May 7, 2024 at 6:22 pm

WONDERFUL Post.thanks for share..extra wait .. ?

Affordable Full Coverage Car Insurance · May 25, 2024 at 11:17 am

wonderful points altogether, you simply received a emblem new reader. What could you recommend in regards to your post that you just made a few days ago? Any sure?

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *