Python Sets

Python Sets


  Machine Learning in Python

Summary : Sets are used to represent group of unique data elements. Typical uses of sets include operations like union, intersection etc.

Contents

 What is a set

The primary purpose of a set is to allow basic set operations like

  • Intersection – Finding out common elements across sets
  • Union – Finding out all the unique elements across sets
  • Difference – What is in A that is not in B

etc

There are a couple of ground rules when it comes to sets.

  • A set can only have unique elements – no duplicates allowed
  • No ordering
  • No index based access

Now, let’s see how to initialize a set. Say, we have different weather patterns across the week and we want to put them into a set.

Set Unique Elements
Set Unique Elements
weather = {"rainy","rainy","sunny","snowy","rainy","sunny","snowy"}
type(weather)


set

Now, if you look at the weather set, you will see that all the duplicate elements re removed and you only get the unique elements.

 weather


{'rainy', 'snowy', 'sunny'}

Create an empty set

Creating empty sets on the other hand is a bit tricky. For example, you might expect that this might create an empty set.

weather = { }

but it does not. It creates an empty dictionary.

type(weather) 

dict

To create an empty dictionary, you use set( ) function

weather = set()
type(weather)

# weather = { } would create an empty dictionary not an empty set
set

 Add elements to a Set

After a set has been created or initialized, adding elements to the set is just as simple. Just use the add( ) function.

weather.add("stormy")
weather


{'stormy'}

Python does not guarantee that elements are added in order ( alphabetical or otherwise). For example, when you add the following unique elements, they don’t get added in any alphabetical order.

weather.add("hot")
 weather.add("warm")
 weather add("foggy")
 weather.add("windy")
 weather.add("rainy")
 weather.add("snowy")
 weather.add("sunny")
 weather.add("cloudy")
 weather.add("cold")
 weather

{'cloudy', 'cold', 'foggy', 'hot', 'rainy', 'snowy', 'sunny', 'warm', 'windy'}

Remove a random element from a set

#pop() - removes a random element
weather.pop()

'foggy'

Remove a particular element from a set

remove( ) is probably a more useful function. It removes a particular element from the set. Since sets are not index based, you would have to specify the actual value. For example, to remove the value “rainy” from the weather set, use

# remove() - removes a particular element
weather.remove("rainy")
weather
{'cloudy', 'cold', 'foggy', 'hot', 'snowy', 'sunny', 'warm', 'windy'}

Clear all the elements in the list

To clear all the elements from a set, use clear( ) function.In [29]:

# clear() - clears all the elements in the list.
weather.clear()
weather

set()

Set Union

These are probably the most used operations on sets. 
– union 
– intersection 
– difference 
Venn diagrams are probably the best choice to understand these visually. Here is a picture to understand set union.

Set Union

Set Union
Set Union
Set Union
odd_num_1 = {1,3 ,5 ,7}
odd_num_2 = {9,11,13,15}

There are 2 different syntax for union.

  • union method
  • pipe ( | ) operator
# via union method
 odd_numbers = odd_num_1.union(odd_num_2)
 odd_numbers

{1, 3, 5, 7, 9, 11, 13, 15}
# via | operator
 odd_numbers = odd_num_1 | odd_num_2
 odd_numbers

{1, 3, 5, 7, 9, 11, 13, 15}

Set Intersection

Intersection between 2 sets gives out what is common between them. Again, in terms of Venn Diagrams, the following picture summarizes intersection of sets.

Intersection
Intersection

Let’s take the same data sets as above.

odd_num_1 = {1,3 ,5 ,7}
odd_num_2 = {7 ,9,11,13}
Set Intersection
Set Intersection

Just as with union, there are 2 different syntax for intersection.

  • intersection method
  • pipe ( & ) operator
# via intersection() method
 odd_numbers = odd_num_1.intersection(odd_num_2)
 odd_numbers

{7}
# via & operator
 odd_numbers = odd_num_1 & odd_num_2
 odd_numbers

{7}

Set Difference

Difference of sets is a bit different. It is an asymmetric operation – Meaning the order of operation is important. You can see why from the picture below. the red shaded part shows the difference between the two sets.

Difference
Difference
odd_num_1 = {1, 3, 5, 7, 9, 11}
odd_num_2 = {1, 3 ,12}

Set Difference

Set Difference

Once again, there are 2 different syntax for intersection.

  • difference method
  • pipe (  ) operator
# via the difference() method
 odd_numbers = odd_num_1.difference(odd_num_2)
 odd_numbers

{5, 7, 9, 11}
# via the - operator
 odd_numbers = odd_num_1 - odd_num_2
 odd_numbers

{5, 7, 9, 11}

Symmetric Difference

Symmetric difference between two sets can be used to compute everything that is not common between the two sets. This is more or less the opposite of of set intersection as you can see from the Venn Diagram below.

Symmetric Difference
Symmetric Difference

Here is another visual to show the same with our sample data.

Set Symmetric Difference
Set Symmetric Difference

The 2 different syntax for symmetric difference are.

  • symmetric_difference method
  • pipe ( ^ ) operator
odd_num_1 = {1, 3, 5, 7, 9, 11}
odd_num_2 = {1, 3 ,7, 13}
# via symmetric_difference()
 odd_numbers = odd_num_1.symmetric_difference(odd_num_2)
 odd_numbers

{5, 9, 11, 13}
# via ^ operator
 odd_numbers = odd_num_1 ^ odd_num_2
 odd_numbers


{5, 9, 11, 13}

Check if an element exists in a set

To check if an element exists in a set, use the in syntax.

Set Element Exits
Set Element Exits
odd_numbers = {1,3,5,7,9}

# evaluates to True
print ( 1 in odd_numbers )

# evaluates to False
print ( 1 not in odd_numbers)
True
False

Delete an entire set

To delete the entire set (as opposed to just remove all the elements ), use the standard python del operator.

Delete an entire set

Delete an entire set
# del key word - used to delete the entire set
del odd_numbers
odd_numbers
NameError                                 Traceback (most recent call last)
<ipython-input-11-f24e43c1dcd7> in <module>
      1 # del key word - used to delete the entire set
      2 del odd_numbers
----> 3 odd_numbers

NameError: name 'odd_numbers' is not defined

discard ( remove ) particular element from the set

odd_numbers = {1, 5, 9, 11, 13}
odd_numbers.discard(1)
odd_numbers

{5, 9, 11, 13}

If it does not exist return nothing. remove() on the other hand raises an error

odd_numbers.remove(1)
odd_numbers

KeyError                                  Traceback (most recent call last)
<ipython-input-14-58450f05369f> in <module>
----> 1 odd_numbers.remove(1)
      2 odd_numbers

KeyError: 1
  1. NO replace method since sets are unordered and hence cannot be indexed. To replace a particular value, use the remove() and add() methods together.
  1. copy() – creates a copy of the set
weather = {"rainy","sunny","snowy"}
weather_c = weather.copy()
weather_c.add("stormy")

# Check that weather and weather_c variable points to different areas in memory
print ( id(weather) )
print ( id(weather_c) )

# and changing one does not change the other
print ( weather )
print ( weather_c)
86244088
86242528
{'snowy', 'sunny', 'rainy'}
{'stormy', 'snowy', 'sunny', 'rainy'}

© Ajay Tech

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.