Sets in Python

In this tutorial, you will learn about the set data structure in python. How to create a set, how to add more data in a set, how to remove data in a set and more set operations.

Set is one of the four built-in data types such as list, tuple, set, dictionary. A set is a data structure used to store unique elements in an unordered fashion. Sets are used to store multiple items in a single variable. Set items are unordered, mutable and has no duplicate elements.

  • Sets are defined by using the curly braces {} or the set() function.

Creating sets:

  1. Using Curly braces:-
    my_set = {1,2,3,4,5}
  2. Using set() function:-
    my_set = set([1,2,3,4,5])
  3. An empty set must be created by using the set() function, as curly braces {} creates an empty dictionary.
    empty_set = set()

Basic Set operations:-

  • Add
  • Remove
  • Discard
  • Clear

In the below example set operations are included: Adding the element and Removing the element.

my_set = {1,2,3,4,5}
my_set.add(6)
my_set.remove(5)
print(my_set)

Output:

{1, 2, 3, 4, 6}

discard():
Discard() method will removes the element if it is found, does nothing if not found(it will not return any error).

clear():
It will empties the set.

Set Operations:

  1. Union:
    Union operator is used to combine the different sets usingĀ  “|” operator or union() method and it will also remove duplicate values.
    Using | operator:

    set1 = {1, 2, 3}
    set2 = {one, two, three}
    union_set = set1 | set2
    print(union_set)
    Output:
    {1, 2, 3, one, two, three}

    Using union() method:

    set1 = {1, 2, 3}
    set2 = {2, 4, 5}
    union_set = set1.union(set2)
    Output:
    {1, 2, 3, 4, 5}
  2. Intersection:
    Intersection operator is used to find the common values between two sets and return those values in a set using the intersection() method or the “&” operator. In this example I have included both the methods.

    set1 = {1, 2, 3, 4, 5}
    set2 = {2, 4, 6, 8}
    intersection_set = set1&set2
    intersection_Set = set1.intersection(set2)
    print(intersection_set)
    print(intersection_Set)
    Output:
    {2, 4}
    {2, 4}
  3. Difference:
    Difference method will display the elements in first set but not in the second set using the “-” operator or difference() method.

    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    difference_set = set1 - set2 
    difference_Set = set1.difference(set2)
    print(difference_set)
    print(difference_Set)
    Output:
    {1, 2}
    {1, 2}
  4. Symmetric Difference:
    It will returns elements in either set but not in both sets using “^” operator and symmetric_difference() method.

    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    symmetric_difference_set = set1 ^ set2
    symmetric_difference_Set = set1.symmetric_difference(set2)
    print(symmetric_difference_set)
    print(symmetric_difference_Set)
    Output:
    {1, 2, 4, 5}
    {1, 2, 4, 5}

 

 

Leave a Comment

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

Scroll to Top