|
|
| Introduction | Variables | If-else |
| Strings | Functions | while-loop |
| For-Loop | List | Set |
| Dictionary | Tuple | Try..Except |
| Class/Object | Inheritance | Polymorphism |
| File Handling | ||
Python set is widely used and popular unordered collection. That means when items are inserted in a python set, they are not inserted in an order. They can be inserted in any positions. They are placed in the set based on the hash value. It can has items with different datatypes. Python does not allow duplicates items in the set. The items in a set cannot be modified. However, its can be deleted and new items can be added. Python set is an unordered collection of multiple items having different datatypes. In Python, sets are mutable, unindexed and do not contain duplicates. The order of elements in a set is not preserved and can change. Python sets can store None values.
There are several ways to create a set in python. The easiest way is to specify the values within the curly braces. For example, {"item1","item2","item3"} That would create a set consisting of "item1", "item2", "item3". Another way to create a set is to use the set() function. You can create a list or tuple in the set() method to create a set. Let's see different ways of creating a set.
Code Example 1:
#Create a set
citiesSet={"Dallas", "Houston", "Chicago"}
#Print
print(citiesSet)
#Output
'Dallas', 'Houston', 'Chicago'
Code Example 2:
#Create set from tuple
myTuple=("Arizona","Nevada","Texas")
stateSet=set(myTuple)
#Print
print(stateSet)
#Output
'Arizona','Nevada','Texas'
Code Example 3:
#Create set from a list
listNames=["David","Roger","Mary","Amy"]
setNames=set(listNames)
#Print
print(setNames)
#Output
{'David', 'Roger', 'Mary', 'Amy'}
Code Example 4:
#create set from a set
listColors={"Orange","Green","Red"}
setColors=set(listColors)
#Print:
print(setColors)
#Output
{'Green', 'Orange', 'Red'}
If you already have a set that contains few elements and you want to add new elements to the existing set, add and update method can be used. Let's see some examples of how to use the add one or more items to an existing set..
Code Example 5:
#Create a set:
setAnimals={"Dog","Cat"}
#Add more animals to set
setAnimals.add("Monkey")
setAnimals.add("Tiger")
#Print
print(setAnimals)
#Output
{'Cat', 'Dog', 'Monkey', 'Tiger'}
Since the items in Python are not ordered, we cannot access each item using the index like we did in List. The best way to access the items in Python Set is to loop through the set. We can use for-loop to access the items. Let's see some examples of looping through Python Set. .
Code Example 6:
#Create a Set
setColors={"Orange","Green","Red","Blue"}
#Access each item in a set
for x in setColors:
print(x)
#Output
Red
Orange
Green
Blue
update() method add new elements to a set. You can add elements from an iterable (like a list, tuple, dictionary, or another set) using update(). The method also ensures that duplicate elements are not added since sets only contain unique elements. .
Code Example 7:
#Create a Set
setA={1,2,3,4}
#Update setA
setA.update([3,5,6])
#print
print(setA)
#Output
{1,2,3,4,5,6}
Sometimes you may need to remove or delete items from a Python set. It may be necessary remove because item in the set my be outdated or is nor longer required. Python provides various ways to delete items in a set. You can use the remove() method and pass the item to be removed. Please make sure to use the remove() method if you are sure that remove method if you know that the item exists in the set. If you use remove() method to remove an item that does not exist, you will get a KeyError. If you are not sure if the item exists in a set, use the discard() method. You can also use pop() method to remove a random item from the set. Use clear() method if you want to remove all the items from the set. Let's see some examples of set.
Code Example 8:
#create as set.
mySet={1,2,3,4,5,6}
#remove()
if(3 in mySet):
mySet.remove(3)
#print
print(mySet)
#Output
{1, 2, 4, 5, 6}
Code Example 9:
#use discard to delete an item
mySet={"one","two","three","four"}
#delete item "three"
mySet.discard("three")
#print
print(mySet)
#Output
{'four', 'two', 'one'}
Code Example 10:
#pop() method to delete a random item
set1 = {10,20,30,40,50}
#pop()
retNum = set1.pop()
#print
print(f"Item removed is: {retNum}")
#Output
{10,20,30,50}
Code Example 11:
#clear() to delete all items
set1.clear()
#print
print(set1)
#Output
None