sets in python

Sets in python

Introduction

“The expert in anything was once a beginner”

In this article, we will discuss a unique data type called sets with all its unique properties. We can also discuss the operators which are unique to sets.

Definition

A Set is an unordered collection(Not indexed) data type which cannot be modified but addition and deletion can be done. Python’s set is the notation of the set in maths. 

The set will not have any duplicate elements.

The set is written within curly brackets

The syntax for creating a set

variable_name={set of values separated by a comma}

Here, the set will be saved in a variable and can be used in the future.

Example

fruits={"apple","orange","pine-apple","grapes"}

Here, fruits are a set containing a list of fruits.

Here, we cannot predict the order of the elements inside the set, because it is not ordered.

Accessing a set

We cannot access the set with the index as there is no index available in the set.

So, we can use for loop to access all the elements available in it.

for i in fruits:
  print(i)
orange
apple
grapes
pine-apple

Modifying elements in a set

Modification is not possible in a set. We cannot modify the existing elements in a set. But, we can add or delete the element.

See also  Built-in Functions and Methods in python

Adding elements in a set

To add one item to a set use the add() method.

fruits={"apple","orange","pine-apple","grapes"}
fruits.add("water-melon")
print(fruits)
{'water-melon', 'pine-apple', 'grapes', 'orange', 'apple'}

To add items from another set into the current set, use the update() method. The object in the update() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries, etc.).

fruits={"apple","orange","pine-apple","grapes"}
fruits.update(["water-melon","kiwi"])
print(fruits)
{'water-melon', 'pine-apple', 'kiwi', 'grapes', 'orange', 'apple'}

Remove elements in a set

remove()

This method is used to delete the specified elements in the set. If the element is not available then the remove method will throw an error.

fruits={"apple","orange","pine-apple","grapes"}
fruits.remove("grapes")
print(fruits)
{'orange', 'apple', 'pine-apple'}

discard()

This method is used to delete the specified elements in the set. If the element is not available then the discard method will not throw an error.

fruits={"apple","orange","pine-apple","grapes"}
fruits.discard("pine-apple")
print(fruits)
{'orange', 'apple', 'grapes'}

pop()

This method is used to delete a random element and save it in a variable.

fruits={"apple","orange","pine-apple","grapes"}
poped_item=fruits.pop()
print(poped_item)
print(fruits)
orange
{'apple', 'grapes', 'pine-apple'}

clear()

The clear() method empties the set.

fruits={"apple","orange","pine-apple","grapes"}
del fruits
print(fruits)
set()

del keyword

The del keyword will delete the set completely.

fruits={"apple","orange","pine-apple","grapes"}
del fruits
print(fruits)
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(fruits)
NameError: name 'fruits' is not defined

Unique operators in set

Union

Union of two sets are used to merge all the elements available in a set. 

This is represented with the help of | operator. The same can be done with the union() method. 

Union is similar to the set theory. 

This allows multiple operands (i.e. sets) and the order of precedence is from left to right.

fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
print(fruits1|fruits2)
print(fruits1.union(fruits2))
{'pine-apple', 'orange', 'water melon', 'apple', 'kiwi'}

Intersection

The intersection is used to result in the common elements available in all the sets. 

See also  Tuples in python

The intersection is done with the symbol &. The same can be done with intersection() method. 

This allows multiple operands (i.e. sets) and order of precedence is from left to right.

fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
print(fruits1&fruits2)
print(fruits1.intersection(fruits2))
{'apple'}

Difference

The difference is used to result in a set with the elements that are present only in the first set. 

The difference is done with the symbol -. The same can be done with the difference() method. 

This allows multiple operands (i.e. sets) and the order of precedence is from left to right.

fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
print(fruits1-fruits2)
print(fruits1.difference(fruits2))
{'orange', 'pine-apple'}
fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
print(fruits2-fruits1)
print(fruits2.difference(fruits1))
{'water melon', 'kiwi'}

Symmetric difference

Symmetric difference return all the elements that are available in either of the set but not both(which means it will ignore the common elements).

The symbol used for the symmetric difference is ^. The same can be done with the symmetric_difference() method. 

This allows multiple operands (i.e. sets) and the order of precedence is from left to right. But the method does not allow multiple operands.

fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
print(fruits1.symmetric_difference(fruits2))
{'orange', 'water melon', 'pine-apple', 'kiwi'}

isdisjoint()

The isdisjoint method returns True if there are no common elements in the two sets. Else returns False.

fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
print(fruits1.isdisjoint(fruits2))#False

issubset()

The issubset returns True is all the elements available in a set is available in another subset.

The symbol used for the issubset is <=. The same can be done with the issubset() method.

fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
fruits3={"apple","orange"}
print(fruits1.issubset(fruits2))#False
print(fruits3.issubset(fruits1))#True
print(fruits1<=fruits2)#False
print(fruits3<=fruits1)#True

Here, we can clearly see that fruits2 is a subset of fruit1.

Here, we also return True if two sets are identical. But when we use < symbol this will return False for identical sets and returns True only for proper subset.

See also  50+ Best python projects with source code [2023]

issuperset

This is just the opposite of issubset.

The symbol used for the issuperset is >=. The same can be done with the issuperset() method.

fruits1={"apple","orange","pine-apple"}
fruits2={"apple","kiwi","water melon"}
fruits3={"apple","orange"}
print(fruits2.issuperset(fruits1))#False
print(fruits1.issuperset(fruits3))#True
print(fruits2>=fruits3)#False
print(fruits1>=fruits3)#True

Here, fruits1 is the super set and fruits2 is the subset.

Here, we also return True if two sets are identical. But when we use > symbol this will return False for identical sets and returns True only for a proper superset.

Practice yourself

  • Create three or more sets and try doing all the set operations and analyzing the report generated.

Conclusion

Thus, we have seen in detail the set. You can fork and use this repl link to explore more about the set in python. You can write your doubts in the comments and can eagerly expect a reply from your friends. Learning is multiplied by sharing.

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.