# Python Sets

If you want to represent a group of unique values as a single entity then you should go for set data type.  in Sets duplicates are not allowed and insertion order is not preserved. But you can sort the object elements but indexing and slicing not allowed.

Set objects are mutable i.e once you creates set object you can do changes in that object based on our requirement. you can represent set elements within curly braces and with comma seperation you can apply mathematical operations like union, intersection, difference etc on set objects.

• Insertion order is not preserved
• Duplicates are not allowed
• Heterogeneous objects are allowed
• Index concept is not applicable
• It is mutable collection
• Growable in nature
```set = {"apple","banana","cherry"}
print(set)

Output

{'apple', 'cherry', 'banana'}
```

## Creation of Set objects

• Using given elements
```s={10,20,30,40}
print(s)
print(type(s))

Output

{40, 10, 20, 30}

<class 'set'>```
• Using set() function

Here is the basic syntax of set() method,

s=set(any sequence)

```list = [10,20,30,40]
s=set(l)
print(s)

Output

{40, 10, 20, 30}```
• Using set() and range() function
```s=set(range(5))
print(s)

Output

{0, 1, 2, 3, 4}```

Note : While creating empty set you have to take special care. It is mandatory you should use set() function.

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

```s={}
print(s)
print(type(s))

Output

{}
<class 'dict'>```
```s=set()
print(s)
print(type(s))

Output

set()
<class 'set'>```

## Accessing Items of Sets

As we said earlier indexing and slicing not allowed in sets. But you can access the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

```set = {"one","two","three"}

for x in set:
print(x)

Output

two
three
one```

## Important functions of Set

A various functions can be used with set data type. Let’s look into some of them,

### 1. Adding Items into set

You can add one item to a set by using add() method.

```set1 = {"one", "two", "three"}
print(set1)

Output

{"four", "two", "three", "one"}```

If you want to add more than one item to a set you should go for update() method.

```set2={"one", "two", "three"}
set2.update(["four", "five", "six"])
print(set2)

Output

{'five', 'three', 'four', 'six', 'one', 'two'}```

### 2. Cloning a set

You can clone a set into other set using copy() method.

```s={10,20,30}
s1=s.copy()
print(s1)```

### 3. Removing items from a set

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

```s={40,10,30,20}
s.remove(30)
print(s)

{40, 10, 20}

s.remove(50)

KeyError: 50```

### 4. Clearing all values from a set

To remove all elements from the Set, you can use clear() method.

```s={10,20,30}
print(s)
s.clear()
print(s)

Output

{10, 20, 30}
set()```

## Mathematical operations on Set

### union()

You can use union function to return all elements present in both sets.

Basic Syntax : x.union(y) or x|y

```x={10,20,30,40}
y={30,40,50,60}

print(x.union(y))
print(x|y)

Output

{10, 20, 30, 40, 50, 60}
{10, 20, 30, 40, 50, 60}```

### intersection()

It returns common elements present in both sets.

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

```x={10,20,30,40}
y={30,40,50,60}

print(x.intersection(y))
print(x&y)

Output

{40, 30}
{40, 30}```

### difference()

It returns the elements present in x but not in y

Basic Syntax : x.difference(y) or x-y

```x={10,20,30,40}
y={30,40,50,60}

print(x.difference(y))
print(x-y)
print(y-x)

Output

{10, 20}
{10, 20}
{50, 60}```

### symmetric_difference()

It returns elements present in either x or y but not in both.

Basic Syntax : x.symmetric_difference(y) or x^y

```x={10,20,30,40}
y={30,40,50,60}

print(x.symmetric_difference(y))
print(x^y)

Output

{10, 50, 20, 60}
{10, 50, 20, 60}```

## Membership operators (in , not in)

Membership operators can be used with sets as well.

```s=set("durga")
print(s)
print('d' in s)
print('z' in s)

Output

{'u', 'g', 'r', 'd', 'a'}
True
False```

## Set Comprehension

Here, set comprehension is possible.

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

print(s)

Output

{0, 1, 4, 9, 16}

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

print(s1)

Output

{16, 256, 64, 4}```

## Set objects don’t support Indexing and Slicing

As we discussed earlier in this guide, python sets don’t support indexing and slicing.

```s1={10,20,30,40}

print(s1)      >>  TypeError: 'set' object does not support indexing
print(s1[1:3])   >>  TypeError: 'set' object is not subscriptable```

Read Also : Differences between List and Tuple in Python

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.