Set in Python

Collection of unordered and unindexed elements.

Declaring a set

my_set={'Alex','Ronald','John'}
print(my_set)
Output
{'Alex', 'Ronald', 'John'}
We can't dsiplay elements based on position as set is unindexed.
my_set={"Alex","Ronald","John"}
print(my_set[1])
Above code will generate error

Displaying all items by looping

We used for loop to display all items of the set.
my_set={'Alex','Ronald','John'}
for i in my_set:
    print(i)
Output is here
Alex
Ronald
John

Searching element

my_set={'Alex','Ronald','John'}
if 'John' in my_set:
 print("Yes, Present in set")
else:
 print("No , not present in set")
Output is here
Yes, Present in set

Add , remove , update elements

We can use add() method to add element
my_set={'Alex','Ronald','John'}
my_set.add('Lewis')
print(my_set)
Output is here
{'Ronald', 'John', 'Alex', 'Lewis'}
We can use update() to add more than one element.
my_set={'Alex','Ronald','John'}
my_set.update(['Lewis','Hawkins','Geek'])
print(my_set)
Output is here
{'Hawkins', 'John', 'Alex', 'Ronald', 'Lewis', 'Geek'}
We can use remove() to delete any element from the set. ( if element is not there , then error will be generated)
my_set={'Alex','Ronald','John'}
my_set.remove('Alex')
print(my_set)
Output is here
{'John', 'Ronald'}
We can use discard() to remove any element. ( if element is not there , no error will be generated )
my_set={'Alex','Ronald','John'}
my_set.discard('Alex')
print(my_set)
We can use pop() to remove last element.
my_set={'Alex','Ronald','John'}
my_set.pop()
print(my_set)
Output is here
{'Alex', 'John'}

Number of elements in the set

len() returns number of elements present in a set
my_set={'Alex','Ronald','John'}
print("Number of elements: ",len(my_set))
Output is here
Number of elements: 3

Deleting set

my_set={'Alex','Ronald','John'}
del my_set
print("Number of elements: ",len(my_set))
The last line will create error as we are deleting the set before this line.

Removing all elements

By using clear(), we can remove all elements
my_set={'Alex','Ronald','John'}
my_set.clear()
print(my_set)
This will empty the set by removing all elements. The output is here
set()

Joint two sets by union()

We will use union() mehtod to join two sets and print final array
my_set1={'Alex','Ronald','John'}
my_set2={'Ron','Geek'}
my_set=my_set1.union(my_set2)
print(my_set)
Output is here
{'Ronald', 'Geek', 'Alex', 'John', 'Ron'}
We can use difference() to find out difference of first and second array with respect to first array.
my_set1={'Alex','Ronald','John'}
my_set2={'Ron','Ronald'}
my_set=my_set1.difference(my_set2)
print(my_set)
Output is here
{'John', 'Alex'}
We can use intersection() to get common element between two or more sets
my_set1={'Alex','Ronald','John'}
my_set2={'Ron','Alex'}
my_set=my_set1.intersection(my_set2)
print(my_set)
Output is
{'Alex'}
With three sets
my_set1={'a','b','c'}
my_set2={'d','b','a'}
my_set3={'f','b','a'}
my_set=my_set1.intersection(my_set2,my_set3)
print(my_set)
Output
{'b', 'a'}
intersection_update() only keeps common elements and removes other.
my_set1={'Alex','Ronald','John'}
my_set2={'Ron','Ronald'}
my_set1.intersection_update(my_set2)
print(my_set1)
Output is here
{'Ronald'}
issubset() returns true if all elements are present inside another set.
my_set1={'a','b','c'}
my_set2={'d','b','a','f','c'}
my_set=my_set1.issubset(my_set2)
print(my_set)
Output
True
issuperset() To get true if all elements of subset present in main set.
my_set1={'d','b','a','f','c'}
my_set2={'a','b','c'}
my_set=my_set1.issuperset(my_set2)
print(my_set)
Output
True


Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer