Set in Python


 Set

        In Python set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of a element in a set is undefined though it may consist of various elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimised method for checking whether a specific element is contained in the set.

Creating a set

      set can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside 'curly braces' { }, separated by 'comma'. A set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed. the order of element in a set is undefined and is unchangeable. Types of elements in a set need not be the same, various mixed up data type values can also be passed to the set.

Example

# creating a set

set=set()

print("initial blank set:")

print(set)

# creating a set with the use of a string

set=set("btech")

print(set)

# creating a set with the use of a list

set=set(["python","for","btech"])

print(set)

# creating  a set with the mixed type of values

set=set([1,2,'btech',4,5,'btech'])

print(set)


Output

initial blank set:
{}

{'b','t,'e','c','h'}             # order may be changed

{'python','for','btech'}    # order may be changed

{1,2,4,5,'btech'}           # order may be changed


Note: set items can not be accessed by referring an index, since sets are unordered the item has no index. But you can loop through the set items using for loop or ask if a specified value is present in a set,by using the "in"keyword.

Example


  1. Days = {"Monday""Tuesday""Wednesday""Thursday""Friday""Saturday""Sunday"}    
  2. print(Days)    
  3. print(type(Days))    
  4. print("looping through the set elements ")    
  5. for i in Days:    
  6.     print(i)    


Output


{'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday', 'Wednesday'}
<class 'set'>
looping through the set elements
Friday
Tuesday
Monday
Saturday
Thursday
Sunday
Wednesday

Adding Elements to the set

Elements can be added to the set by using built-in add() function. Only one element at a time can be added to the set by using add() method. For addition of two or more elements at once update() method is used.


Example

# creating a empty set

set=set()

print(set)

# adding elements and tuple to the set

set.add(8)

set.add(9)

set.add((6,7))

# adding elements using update method

set.update([10,11])

print(set)


Output

{}

{8,9,(6.7)}                   # order may be changed

{8,9,10,11,(6,7)}         # order may be changed


Removing elements from the set

          Elements can be removed from the set by using built-in remove() function but a key error arises if element does not exist in the set.To remove elements from the set without key error use discard() and pop() method. but the pop() method remove only the last element from the set. To remove all the set elements from the set at once clear() method is used.


Example

# creating a set

set=set([1,2,3,4,5,6,7,8,9,10,12,12])

print("initial set:")

print(set)

# using remove() method

set.remove(5)

set.remove(6)

print(set)

# using discard() method

set.discard(8)

set.discard(9)

print(set)

# suing pop() method

set.pop()

print(set)

# using clear() method

set.clear()

print(set)


Output

initial set:

{1,2,4,5,6,3,7,9,10,8,12}     # order may be changed

{1,3,2,6,8,12,4,7,9,10}       # order may be changed

{3,2,1,10,12,7,6,4}             # order may be changed

{3,2,1,10,12,7,6}                # order may be changed

{}








       

No comments:

Post a Comment