DEV Community

Cover image for Learn Python: Sets
Rishi
Rishi

Posted on

10 1

Learn Python: Sets

A set is another type of collection like list and tuple.

So what are sets?

  • Sets are unordered – Items stored in a set aren’t kept in any particular order.
  • Set items are unique – Duplicate items are not allowed.
  • Sets are unindexed – You cannot access set items by referring to an index.
  • Sets are changeable (mutable) – They can be changed in place, can grow and shrink on demand.

The values of a set are defined within curly braces { }.
A quick reminder:

  • For list, we use square brackets [ ].
  • For tuple, we use brackets ( ).
fruits = {'apple','orange'}
print(fruits);
Enter fullscreen mode Exit fullscreen mode

Sets don't maintain order. Every time we execute the code, the order of the values in the set changes.

Do note, in the example below we are adding pineapple multi times, but when we print the set, we only see pineapple once. This is because set does not allow duplicate.

fruits = {'apple','orange'}
fruits.add('pineapple');
fruits.add('pineapple');
fruits.add('pineapple');
print(fruits);
Enter fullscreen mode Exit fullscreen mode



Benefits of sets

The key benefit of sets is that they make comparing values between two sets easier.

Set Operations

Sets are commonly used for computing mathematical operations such as intersection, union, difference, and symmetric difference.

Difference

basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Difference  
basket_1_difference = basket_1.difference(basket_2);
basket_2_difference = basket_2.difference(basket_1);

print(basket_1_difference);
print(basket_2_difference);
Enter fullscreen mode Exit fullscreen mode

Symmetric Difference

basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Symmetric Difference 
basket_1_symmetric_difference = basket_1.symmetric_difference(basket_2);
basket_2_symmetric_difference = basket_2.symmetric_difference(basket_1);
print(basket_1_symmetric_difference);
print(basket_2_symmetric_difference);
Enter fullscreen mode Exit fullscreen mode

Intersection

basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Intersection
basket_1_intersection = basket_1.intersection(basket_2);
print(basket_1_intersection);
Enter fullscreen mode Exit fullscreen mode

Union

basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Union
basket_1_union = basket_1.union(basket_2);
print(basket_1_union);
Enter fullscreen mode Exit fullscreen mode



👋 One new thing before you go

Are you investing in your developer career? 😒

We have created a membership program that helps cap your costs so you can build and experiment for less. And we currently have early-bird pricing which makes it an even better value! 🐥

Just one of many great perks of being part of the network ❤️

Top comments (5)

Collapse
 
waylonwalker profile image
Waylon Walker

Sets are one of my favorite container types. You can do really powerful operations with very little effort, and they are wicked fast at it.

The one thing that I see your missing is the set operators like + * - | ^

Collapse
 
rishiabee profile image
Rishi

Waylon, Thanks!
Will definitely include that in another article about advance features of sets.

Much appreciated. Thanks.

Collapse
 
waylonwalker profile image
Waylon Walker

As much as I like the tersness of the operators, and being able to quickly compare datasets ad-hoc, using the method names can be much more readable and less confusing. Good call to use those. I guess its worth a mention because you will run into them into the wild.

Collapse
 
ben profile image
Ben Halpern

Love the use of replit here

Collapse
 
rishiabee profile image
Rishi

Javier,
Thank for your input, much appreciated.
I'm taking note, I'll definitely add that when I review it.

Again, thanks!

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay