sets
July 3, 2024
[1]: s = {}
[2]: type(s) #Please check Dictionary is a another data structure in Python and␣
↪declaryion of both set and dcit are same but usage is different
[2]: dict
[4]: #just think of how to initialize empty set
[3]: s1 = {1,2,3,4,5}
[5]: type(s1)
[5]: set
[7]: s2 = {1,1,12,3,3,3,4,5,5,5,55,523,34,3,45,6,67}
[8]: s2 # In sets there will be no duplicates and will be automatically␣
↪arreanged in acesding order
# As the insertion is in order it will take more time(O(logn)) then␣
↪appeding it to list(O(1))
#we will discuss about time complexity in detailed later
[8]: {1, 3, 4, 5, 6, 12, 34, 45, 55, 67, 523}
[9]: l = list(s2) #casting into list
l
[9]: [1, 34, 3, 4, 5, 6, 67, 523, 12, 45, 55]
[10]: tuple(s2) #casting into tuple
[10]: (1, 34, 3, 4, 5, 6, 67, 523, 12, 45, 55)
[11]: l
[11]: [1, 34, 3, 4, 5, 6, 67, 523, 12, 45, 55]
1
[12]: set(l)
[12]: {1, 3, 4, 5, 6, 12, 34, 45, 55, 67, 523}
[13]: s4 = {1,2,3,4,[1,2,3,4]} #sets are mutable but not the elements of set that␣
↪means a mutable data strcuture cant be stored are element in sets
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-c88b0db15002> in <cell line: 1>()
----> 1 s4 = {1,2,3,4,[1,2,3,4]}
TypeError: unhashable type: 'list'
[14]: s5 = {1,2,3,4, (1,2,3,4)} #As tuples are immutable, it can be stored as element␣
↪of set
[15]: s5
[15]: {(1, 2, 3, 4), 1, 2, 3, 4}
[16]: s6 = {"sumen" , "Sumen" , 2,3,4,5} #we have to take care of case sensitivity␣
↪while storing in sets
[17]: s6
[17]: {2, 3, 4, 5, 'Sumen', 'sumen'}
[39]: s7 = {"sumen" , "sumen" , 2,3,4,5}
[19]: s7
[19]: {2, 3, 4, 5, 'sumen'}
[20]: s7[0] #unlike strings, list and tuples sets are not indexable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-d0678b25f3b2> in <cell line: 1>()
----> 1 s7[0] #unlike strings, list and tuples sets are not indexable
TypeError: 'set' object is not subscriptable
[21]: s7[::-1]
2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-d62874945df9> in <cell line: 1>()
----> 1 s7[::-1]
TypeError: 'set' object is not subscriptable
[22]: s7
[22]: {2, 3, 4, 5, 'sumen'}
[24]: for i in s7 : #if you want to acess the elemets of sets then it is the way
print(i)
2
3
4
5
sumen
[25]: s7
[25]: {2, 3, 4, 5, 'sumen'}
[40]: s7.add(34) #just think why its getting added in middle
[41]: s7
[41]: {2, 3, 34, 4, 5, 'sumen'}
[42]: s7.add(2)
[43]: s7
[43]: {2, 3, 34, 4, 5, 'sumen'}
[44]: len(s7)
[44]: 6
[45]: s7.pop() #pop will delete smallest element of set keep in mind
[45]: 2
[46]: s7
3
[46]: {3, 34, 4, 5, 'sumen'}
[47]: s7.pop() #pop will delete smallest element of set keep in mind
[47]: 3
[48]: s7
[48]: {34, 4, 5, 'sumen'}
[49]: s7.pop()
[49]: 4
[50]: s7.pop()
[50]: 5
[51]: s7
[51]: {34, 'sumen'}
[52]: s7.clear()
[53]: s7
[53]: set()
[54]: s8 = {1,2,3,4}
s9 = {1,2,3,5}
[55]: s8.difference(s9)
[55]: {4}
[56]: s9.difference(s8)
[56]: {5}
[57]: s9.union(s8)
[57]: {1, 2, 3, 4, 5}
[59]: s9.intersection(s8) #all set operaation that we studied in you school, all are␣
↪possbile
[59]: {1, 2, 3}
4
[ ]: