Python Set

 

Python Set

A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.

Creating a set

The set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also provides the set() method, which can be used to create the set by the passed sequence.

Python Set Operations

Set can be performed mathematical operation such as union, intersection, difference, and symmetric difference. Python provides the facility to carry out these operations with operators or methods. We describe these operations as follows.

Union of two Sets

The union of two sets is calculated by using the pipe (|) operator. The union of the two sets contains all the items that are present in both the sets.

Python Set

Intersection of two sets

The intersection of two sets can be performed by the and & operator or the intersection() function. The intersection of the two sets is given as the set of the elements that common in both sets.

Python Set

Difference between the two sets

The difference of two sets can be calculated by using the subtraction (-) operator or intersection() method. Suppose there are two sets A and B, and the difference is A-B that denotes the resulting set will be obtained that element of A, which is not present in the set B.

Python Set

Symmetric Difference of two sets

The symmetric difference of two sets is calculated by ^ operator or symmetric_difference() method. Symmetric difference of sets, it removes that element which is present in both sets. Consider the following example:

Python Set

Python Built-in set methods

Python contains the following methods to be used with the sets.

SN

Method

Description

1

add(item)

It adds an item to the set. It has no effect if the item is already present in the set.

2

clear()

It deletes all the items from the set.

3

copy()

It returns a shallow copy of the set.

4

difference_update(....)

It modifies this set by removing all the items that are also present in the specified sets.

5

discard(item)

It removes the specified item from the set.

6

intersection()

It returns a new set that contains only the common elements of both the sets. (all the sets if more than two are specified).

7

intersection_update(....)

It removes the items from the original set that are not present in both the sets (all the sets if more than one are specified).

8

Isdisjoint(....)

Return True if two sets have a null intersection.

9

Issubset(....)

Report whether another set contains this set.

10

Issuperset(....)

Report whether this set contains another set.

11

pop()

Remove and return an arbitrary set element that is the last element of the set. Raises KeyError if the set is empty.

12

remove(item)

Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.

13

symmetric_difference(....)

Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.

14

symmetric_difference_update(....)

Update a set with the symmetric difference of itself and another.

15

union(....)

Return the union of sets as a new set.
(i.e. all elements that are in either set.)

16

update()

Update a set with the union of itself and others.

 


0 Comments