Set - Set operations

A set is an object that stores a collection of data in the same way as mathematical sets. Here are some important things to know about sets:

·         All the elements in a set must be unique. No two elements can have the same value.

·         Sets are unordered, which means that the elements in a set are not stored in any particular order.

·         The elements that are stored in a set can be of different data types.

To create a set, you have to call the built-in set function.

myset = set()

After this statement executes, the myset variable will reference an empty set.

One argument can be passed to the set function. The argument that you pass must be an object that contains iterable elements, such as a list, a tuple, or a string.

Here is an example:

myset = set(['a', 'b', 'c'])

In this example we are passing a list as an argument to the set function. After this statement executes, the myset variable references a set containing the elements 'a', 'b', and 'c'.

If a string is passed as an argument to the set function, each individual character in the string becomes a member of the set.

Here is an example:

myset = set('abc')

After this statement executes, the myset variable will reference a set containing the elements 'a', 'b', and 'c'.

Sets cannot contain duplicate elements. If an argument containing duplicate elements is passed to the set function, only one of the duplicated elements will appear in the set.

myset = set('aaabc')

The character 'a' appears multiple times in the string, but it will appear only once in the set. After this statement executes, the myset variable will reference a set containing the elements 'a', 'b', and 'c'.

Getting the Number of Elements in a Set

As with lists, tuples, and dictionaries, the len function can be used to get the number of elements in a set.

Example:

>>> myset = set([1, 2, 3, 4, 5])

>>> len(myset)

5

Adding and Removing Elements

Sets are mutable objects, so items can be added to them and items can be removed from them. The add method to add an element to a set.

Example:

>>> myset = set( )

>>> myset.add(1)

>>> myset.add(2)

>>> myset.add(3)

>>> myset e

{1, 2, 3}

Above example creates an empty set and add elements to the set.

A group of elements can be added to a set all at one time with the update method. When a call to the update method is made an argument is passed an object that contains iterable elements, such as a list, a tuple, string, or another set. The individual elements of the object that is passed as an argument become elements of the set.

Example:

>>> myset = set([1, 2, 3])

>>> myset.update([4, 5, 6])

>>> myset e

{1, 2, 3, 4, 5, 6}

Removing an item from set

An item can be removed from a set with either the remove method or the discard method. The item to be removed is passes as an argument to either method, and that item is removed from the set. The only difference between the two methods is how they behave when the specified item is not found in the set. The remove method raises a KeyError exception, but the discard method does not raise an exception.

Example:

>>> myset = set([1, 2, 3, 4, 5])

>>> myset.remove(1)

>>> myset

{2, 3, 4, 5}

>>> myset.discard(5)

>>> myset

{2, 3, 4}

>>> myset.discard(99)

>>> myset.remove(99)

Traceback (most recent call last):

File "<pyshell#12>", line 1, in <module>

myset.remove(99)

KeyError: 99

To clear all the elements from the set, clear method is used.

Example:

>>> myset = set([1, 2, 3, 4, 5])

>>> myset.clear( )

>>> myset

set( )

Using the for Loop to Iterate over a Set

The for loop in the following general format can be used to iterate over all the elements in a set:

for var in set:

statement

statement

etc.

This loop iterates once for each element in the set. Each time the loop iterates, var is assigned an element.

>>> myset = set(['a', 'b', 'c'])

>>> for val in myset:

print(val)

a

c

b

Using the in and not in Operators to Test for a Value in a Set

in operator can be used to determine whether a value exists in a set. not in operator can be used to determine whether a value does not exists in a set.

Example:

>>> myset = set([1, 2, 3])

>>> if 1 in myset:

print('The value 1 is in the set.')

>>> if 99 not in myset:

print('The value 99 is not in the set.')

Finding the Union of Sets

The union of two sets is a set that contains all the elements of both sets. In Python, the union method is used to get the union of two sets. Here is the general format:

set1.union(set2)

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1.union(set2)

>>> set3

{1, 2, 3, 4, 5, 6}

The | operator can also be used to find the union of two sets. Here is the general format of an expression using the | operator with two sets:

set1 | set2

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1 | set2

>>> set3

{1, 2, 3, 4, 5, 6}

Finding the Intersection of Sets

The intersection of two sets is a set that contains only the elements that are found in both sets. In Python, the intersection method is used to get the intersection of two sets.

Here is the general format:

set1.intersection(set2)

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1.intersection(set2)

>>> set3

{3, 4}

The & operator also can be used to find the intersection of two sets. Here is the general format of an expression using the & operator with two sets:

set1 & set2

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1 & set2

>>> set3

{3, 4}

Finding the Difference of Sets

The difference of set1 and set2 is the elements that appear in set1 but do not appear in set2. In Python, the difference method call is used to get the difference of two sets.

Here is the general format:

set1.difference(set2)

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1.difference(set2)

>>> set3

{1, 2}

The – operator can also be used to find the difference of two sets. Here is the general format

of an expression using the - operator with two sets:

set1 − set2

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1 − set2

>>> set3

{1, 2}

Finding the Symmetric Difference of Sets

The symmetric difference of two sets is a set that contains the elements that are not shared by the sets. In other words, it is the elements that are in one set but not in both. In Python, the symmetric_difference method call is used to get the symmetric difference of two sets. Here is the general format:

set1.symmetric_difference(set2)

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1.symmetric_difference(set2)

>>> set3

{1, 2, 5, 6}

The ^ operator can also be used to find the symmetric difference of two sets. Here is the general format of an expression using the ^ operator with two sets:

set1 ^ set2

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([3, 4, 5, 6])

>>> set3 = set1 ^ set2

>>> set3

{1, 2, 5, 6}

Finding Subsets and Supersets

In Python, the issubset method call is used to determine whether one set is a subset of another. Here is the general format:

set2.issubset(set1)

The issuperset method call is used to determine whether one set is a superset of another. Here is the general format:

set1.issuperset(set2)

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([2, 3])

>>> set2.issubset(set1)

True

>>> set1.issuperset(set2)

True

The <= operator can also be used to determine whether one set is a subset of another and the >= operator to determine whether one set is a superset of another.

Here is the general format of an expression using the <= operator with two sets:

set2 <= set1

Here is the general format of an expression using the >= operator with two sets:

set1 >= set2

Example:

>>> set1 = set([1, 2, 3, 4])

>>> set2 = set([2, 3])

>>> set2 <= set1

True

>>> set1 >= set2

True

>>> set1 <= set2

False