Important Notice:

Course Content
Set Data Type in Python (Chapter-14) M3-R5
About Lesson

Note:-

  • Empty Set:-

            Set के elements को curly braces({}) में लिखा जाता है लेकिन अगर empty curly braces({}) का इस्तेमाल किया जाता है तो वो ‘dictionary’ type हो जाता है। Empty Set के लिए empty set() function को देना पड़ता है।

set1 = {}

print(type(set1))                                             # <class ‘dict’> 

set2 = set()

print(type(set2))                                              # <class ‘set’>

 

  • Set data type में duplicate data allow नही होता है यह दूसरे duplicate data को remove कर देता है।

s1=set([1,2,3,5,2,3,1])

print(s1)                                     # {1, 2, 3, 5}

 

  • Set data type में insertion order preserved नही होता है लेकिन element की sorting की जा सकती है।

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

print(s1)                               # {50, 20, 40, 10, 30}

 

  • Set data type में indexing & slicing allow नही है

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

print(s1[2])

print(s1[1:4])                    # TypeError: ‘set’ object is not subscriptable

 

  • Set data type में heterogeneous element allow है

s1={10,15.02,’ak it solution’}

print(s1)                              # {10, ‘ak it solution’, 15.02}

 

Question:    write a python script to remove duplicate elements from a list

l1=[10,20,30,10,20,50,40,60]

l1=list(set(l1))

print(l1)

 

Note:-

  • Empty Set:-

           The elements of a set are written in curly braces({}) but if empty curly braces({}) are used then it becomes ‘dictionary’ type. For Empty Set empty set() function has to be given.

set1 = {}

print(type(set1))                                             # <class ‘dict’> 

set2 = set()

print(type(set2))                                              # <class ‘set’>

 

  • Duplicate data is not allowed in Set data type, it removes other duplicate data.

s1=set([1,4,3,2,5,1,4,3,5])

print(s1)                                     # {1, 2, 3,4, 5}

 

  • Insertion order is not preserved in Set data type but sorting of elements can be done.

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

print(s1)                                               # {50, 20, 40, 10, 30}

 

  • Indexing and slicing is not allowed in Set data type.

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

print(s1[2])

print(s2[1:4])                                       # TypeError: ‘set’ object is not subscriptable

 

  • Set data type allow in heterogeneous element

s1={10,15.02,’ak it solution’}

print(s1)                                                 # {10, ‘ak it solution’, 15.02}

error: Content is protected !!