Python - Set Data Structure
Python - Set Data Structure
Table of Contents
1. Set data structure ............................................................................................................................... 2
2. When should we go for set? .............................................................................................................. 3
3. Creating set by using same type of elements.................................................................................... 3
4. Creating set by using different type of elements .............................................................................. 4
5. Creating set by range(p) type of elements ........................................................................................ 4
6. Creating set by using set(p) predefined function .............................................................................. 5
7. Empty set ............................................................................................................................................ 6
8. len(p) function .................................................................................................................................... 7
9. Methods in set data structure ........................................................................................................... 8
9.1. add(p) method: ............................................................................................................................ 9
9.2. remove(p) method ..................................................................................................................... 10
9.3. clear() method ........................................................................................................................... 10
10. Membership operators: (in and not in) ......................................................................................... 11
11. set comprehensions ....................................................................................................................... 12
12. Remove duplicates in list elements ............................................................................................... 12
Note:
✓ set is a predefined class in python.
✓ Once if we create set object means internally object is creating for set
class.
Note:
✓ Inside set every object can be separated by comma separator.
Note
✓ We can create set by using curly braces {} and all elements separated by
comma separator in set.
Output
{40, 10, 20, 30}
<class 'set'>
Output
{40, 10, 'Daniel', 'Prasad', 30.9}
<class 'set'>
s = set(range(5))
print(s)
output
{0, 1, 2, 3, 4}
Output
{40, 10, 20, 30}
<class 'set'>
Make a note
r = range(0, 10)
l = set(r)
print(l)
output
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
7. Empty set
s = {}
print(s)
print(type(s))
Output
{}
<class 'dict'>
s = set()
print(s)
print(type(s))
Output
set()
<class 'set'>
8. len(p) function
Output
5
Output
5
print(dir(set))
output
[
Important methods
Important point
✓ add(p) method
✓ remove(p) method
✓ clear() method
Output
{40, 10, 20, 30}
Output
{40, 10, 20}
s = {10,20,30}
print(s)
s.clear()
print(s)
Output
{10, 20, 30}
set()
Program in operator
Name demo14.py
s = {1, 2, 3, 'daniel'}
print(s)
print(1 in s)
print('z' in s)
Output
{1, 2, 3, 'daniel'}
True
False
✓ set comprehensions represents creating new set from Iterable object like
a list, set, tuple, dictionary and range.
✓ set comprehensions code is very concise way.
output
{0, 1, 4, 9, 16}
✓ We can remove duplicates elements which are exists in list by passing list
as parameter to set function
Output
{40, 10, 20, 30}