## Python Set with Example – Python Tutorial

###### 1 year ago Lalit Bhagtani 0

In Python set is an unordered collection of unique immutable elements. Every set data structure has three main characteristics, they are as follows :-

1. Unordered :- Set is an unordered collection, which means it does not record position of any element or order of insertion of elements in the set object. Thus set does not support behaviour like indexing, slicing etc, supported by other sequences like list, string or tuple.
2. Unique :- In set, all elements are unique ( no duplicates ). If we try to add an element, which is already present in the set object. It won’t add that element in the set. This characteristics of set is used for removing duplicates from a sequence.
3. Immutable :- In set, all elements must be of immutable type like string, tuple, integer, float. As only immutable type object has hash value.

In python Set is a mutable data structure like list or dictionary, which means we can add or remove elements from set object. As set is a mutable type, it has no hash value. Thus it cannot be use as an element of another set or key of a dictionary object. Set can be used for different type of operations like removing duplicates from a sequence or performing mathematical operations such as union, intersection, and symmetric difference.

In this tutorial, we will learn about the following:

1. Creating Set
3. Remove Element
4. Nesting Set

## Creating Set

In Python set are created by enclosing sequence of elements separated by commas inside curly braces “{ }” or by using set( ) function.

Syntax :    set([ iterable ]) : <set object>

This built-in function returns a new set object, which contain all the unique elements of the iterable object ( passed as an argument ). All the elements of the iterable object must be immutable. If iterable object is not passed, then a new empty set object is returned. For example :-

```# creating set with same data type
s = {1,2,3,4,5}
print(s) -> {1, 2, 3, 4, 5}

# creating set with different data type
s = {'Jan',2.0,True}
print(s) -> {True, 2.0, 'Jan'}

# creating empty set
s = set()
print(s) -> set()
# we can't create empty set by using empty curly braces, as it created empty dictionary

# creating set with set() builtin function by passing string as an argument
s = set('codedestine')
print(s) -> {'e', 'i', 'o', 'c', 'd', 's', 'n', 't'}

# creating set with set() builtin function by passing list
s = set([1,1,2,2,3,4,5])
print(s) -> {1, 2, 3, 4, 5}

# creating set with set() builtin function by passing tuple
s = set((1,2,3,4,5,5,6,7))
print(s) -> {1, 2, 3, 4, 5, 6, 7}

# creating set with set() builtin function by passing dictionary
s = set({'key1':'1','key2':'2','key3':'3'})
print(s) -> {'key2', 'key3', 'key1'}

# creating set with set() builtin function by passing enumerate
s = set(enumerate(['c','o','d','e','d','e','s','t','i','n','e',]))
print(s) -> {(2, 'd'), (3, 'e'), (9, 'n'), (10, 'e'), (6, 's'), (4, 'd'), (1, 'o'), (0, 'c'), (7, 't'), (5, 'e'), (8, 'i')}
```

Set’s add() method can be used to insert single element in the set object and update() method can be used to insert multiple elements in the set object.

update(< Iterable Object >)

[datacamp_exercise lang=”python” height=”370″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create empty set object and assign it to variable s
s = set()

# add element 1 in the set object

# add element 2 in the set object

# print all elements of set
print(s)

# add all the elements of the list [3,4,5,6]
s.update([3,4,5,6])

# print all elements of set
print(s)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

## Remove Element

Set’s remove() and discard() method can be used to remove single element from the set object. The difference between the two method is that, while using remove() method, if element is not present in the set, it raise a KeyError. But discard() method do nothing in such cases.
Syntax:   remove(< Immutable Object >)

[datacamp_exercise lang=”python” height=”450″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create set object and assign it to variable s
s = {1,2,3,4,5}

# remove element 2 from the set object
s.remove(2)

# print all elements of set
print(s)

# discard element 3 from the set object

# print all elements of set
print(s)

# discard element 6 from the set object

# print all elements of set
print(s)

# remove element 6 from the set object
s.remove(6)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

## Nesting Set

In python set cannot be created inside another set, since set is a mutable type and elements of set can only be of immutable type like string, tuple, integer, float. For this reason, python has one more type of set object called as frozenset. A frozenset is like a set data type but it is immutable, which means that once it is created, its elements cannot be altered (adding new element or removing existing element). Therefore it can be used as a dictionary key or as an element of another set. For example:
[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create three frozenset object and assign it to variable fs1,fs2,fs3
fs1 = frozenset([1,2,3,4])
fs2 = frozenset([5,6,7,8])
fs3 = frozenset([9,10,11,12])

# create a set of frozenset
s = {fs1,fs2,fs3}

# print the value of set s
print(s)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

## References :-

That’s all for Python Set with Example. If you liked it, please share your thoughts in comments section and share it with others too.

Next