__Set Data Structure__

- If we want to represent a group of unique values as a single entity then we should go for set.
- Duplicates are not allowed.
- Insertion order is not preserved. But we can sort the elements.
- Indexing and slicing not allowed for the set.
- Heterogeneous elements are allowed.
- Set objects are mutable i.e. once we creates set object we can perform any changes in that object based on our requirement.
- We can represent set elements within curly braces and with comma separation
- We can apply mathematical operations like union,intersection,difference etc. on set objects.
^{}

__Creation of Set objects:__

Ex:

s={10,20,30,40}

print(s)

print(type(s))

Output

{40, 10, 20, 30}

**We can create set objects by using set() function**

s = set(any sequence)

**Ex 1:**l = [10,20,30,40,10,20,10]

s=set(l)

print(s) # {40, 10, 20, 30}

**Ex 2:**s=set(range(5))

print(s) #{0, 1, 2, 3, 4}

**Note:**

- While creating empty set we have to take special care.
- Compulsory we should use set() function.

s = {} ==>It is treated as dictionary but not empty set.

**Ex:**s={}

print(s)

print(type(s))

Output

{}

**Ex:**s=set()

print(s)

print(type(s))

Output

set()

__Important functions of set:__

__add(x):__

Adds item x to the set

Ex:

s={10,20,30}

s.add(40);

print(s) #{40, 10, 20, 30}

**update(x,y,z):**

- To add multiple items to the set.
- Arguments are not individual elements and these are Iterable objects like List,range etc.
- All elements present in the given Iterable objects will be added to the set.

**Ex:**s={10,20,30}

l=[40,50,60,10]

s.update(l,range(5))

print(s)

Output

{0, 1, 2, 3, 4, 40, 10, 50, 20, 60, 30}

**What is the difference between add() and update() functions in set?**

- We can use add() to add individual item to the Set, where as we can use update() function to add multiple items to Set.
- add() function can take only one argument whereas update() function can take any number of arguments but all arguments should be iterable objects.

Q. Which of the following are valid for set s?

s.add(10)

s.add(10,20,30) TypeError: add() takes exactly one argument (3 given)

s.update(10) TypeError: ‘int’ object is not iterable

s.update(range(1,10,2),range(0,10,2))

copy():Returns copy of the set.

It is cloned object.

s={10,20,30}

s1=s.copy()

print(s1)

pop():It removes and returns some random element from the set.

**Ex:**s={40,10,30,20}

print(s)

print(s.pop())

print(s)

Output

{40, 10, 20, 30}

40

{10, 20, 30}

**remove(x):**

- It removes specified element from the set.
- If the specified element not present in the Set then we will get KeyError

s={40,10,30,20}

s.remove(30)

print(s) # {40, 10, 20}

s.remove(50) ==>KeyError: 50

**discard(x):**

- It removes the specified element from the set.
- If the specified element not present in the set then we won’t get any error.

s={10,20,30}

s.discard(10)

print(s) ===>{20, 30}

s.discard(50)

print(s) ==>{20, 30}

clear():To remove all elements from the Set.

s={10,20,30}

print(s)

s.clear()

print(s)

Output

{10, 20, 30}

set()

__Mathematical operations on the Set:__

union():x.union(y) ==>We can use this function to return all elements present in both sets

x.union(y) or x|y

Ex:

x={10,20,30,40}

y={30,40,50,60}

print(x.union(y)) #{10, 20, 30, 40, 50, 60}

print(x|y) #{10, 20, 30, 40, 50, 60}

intersection():x.intersection(y) or x&y

**Returns common elements present in both x and y**

**Ex:**x={10,20,30,40}

y={30,40,50,60}

print(x.intersection(y)) #{40, 30}

print(x&y) #{40, 30}

difference():- x.difference(y) or x-y - returns the elements present in x but not in y

**Ex:**x={10,20,30,40}

y={30,40,50,60}

print(x.difference(y)) #{10, 20}

print(x-y) #{10, 20}

print(y-x) #{50, 60}

symmetric_difference():- x.symmetric_difference(y) or x^y - Returns elements present in either x or y but not in both

**Ex.**x={10,20,30,40}

y={30,40,50,60}

print(x.symmetric_difference(y)) #{10, 50, 20, 60}

print(x^y) #{10, 50, 20, 60}

**Membership operators:(in , not in)**

**Ex:**s=set(“durga”)

print(s)

print(‘d’ in s)

print(‘z’ in s)

Output

{‘u’, ‘g’, ‘r’, ‘d’, ‘a’}

True

False

**Set Comprehension:**

Set comprehension is possible.

s={x*x for x in range(5)}

print(s) #{0, 1, 4, 9, 16}

s={2**x for x in range(2,10,2)}

print(s) #{16, 256, 64, 4}

**set objects won’t support indexing and slicing:**

Ex:

s={10,20,30,40}

print(s[0]) ==>TypeError: ‘set’ object does not support indexing

print(s[1:3]) ==>TypeError: ‘set’ object is not subscriptable