Loading...

Python Sets


A set is an unordered (there is no sequence of items in which they will appear) and unindexed (items positions unknown) collection of elements (items). each items in the collection is unique (distinct). it removes duplicates items implicitly

How to create Sets in python ?


we create a set by using curly brackets { }.
for example:
        		     					    		
shoppset = {"iphone 8", "samsung led tv", "rolex watch", "hp laptop", "iphone 8", "rolex watch"}

print(shoppset)

output:

{'samsung led tv', 'hp laptop', 'rolex watch', 'iphone 8'}

        		     					    		
        		     					    	
in the above example we have assign 'iphone 8', 'rolex watch' two times but it it includes in the set at run time only one item of each type in it.

we can also create a set in python by using the built-in function set().
by using set() function we can make a set from list, tuples.
for example: a set from passing a list in it.

        		     					    		
shopplist = ["iphone 8", "samsung led tv", "rolex watch", "hp laptop", "iphone 8", "rolex watch"]

shoppset = set(shopplist)

print(shoppset)

output:

{'rolex watch', 'samsung led tv', 'hp laptop', 'iphone 8'}

        		     					    		
        		     					    	
for example: a set from passing a tuple in it.
        		     					    		
shopptuple = ("iphone 8", "samsung led tv", "rolex watch", "hp laptop", "iphone 8", "rolex watch")

shoppset = set(shopptuple)

print(shoppset)

output:

{'rolex watch', 'iphone 8', 'hp laptop', 'samsung led tv'}

        		     					    		
        		     					    	

How to Access items in Sets ?


in sets we can not access the items just like in the lists, tuples on the basis of their index position numbers.
set is an unordered (there is no sequence of items in which they will appear) and unindexed (items positions unknown) collection.
because of this reason there is no index to refer the items in it.

but we can check that the item (which we want to query in the set) is present in the set by using the 'in' keyword.
we can iterate the set items by using the 'for' loop.
for example: iterate the set by using 'for' loop
        		     					    		
shoppset = {"iphone 8", "samsung led tv", "rolex watch", "hp laptop"}

for v in shoppset:
	print(v)

output:

iphone 8
samsung led tv
hp laptop
rolex watch

        		     					    		
        		     					    	
for example: check the item present in the set by using 'in' keyword
        		     					    		
shoppset = {"iphone 8", "samsung led tv", "rolex watch", "hp laptop"}

query = "iphone 8"

print(query in shoppset)

output:

True

        		     					    		
        		     					    	
in the above example 'iphone 8' item is checked in the set.
it returns true because set have the 'iphone 8' item in it. if set does not have the item in it then return False.


How to Change items in Sets ?


we can not change items in the set on the basis of their index because set is unordered and unindexed.
but set is mutable we can add new items in it.

Add items in Sets


we can add items in th set by using following ways :

by using built-in add() function.
add() function is used for add only one item in the set.
for example:
        		     					    		
shoppset = {"iphone 8", "samsung led tv", "rolex watch", "hp laptop"}

shoppset.add("iphone 11 pro")

print(shoppset)

output:

{'hp laptop', 'iphone 8', 'iphone 11 pro', 'rolex watch', 'samsung led tv'}

        		     					    		
        		     					    	
by using built-in update() function.
update() function is used to add more than one item in the set.
for example:
        		     					    		
shoppset = {"iphone 8", "samsung led tv", "rolex watch", "hp laptop"}

shoppset.update(["iphone 11 pro", "samsung s20", "airpods"])

print(shoppset)

output:

{'samsung led tv', 'airpods', 'hp laptop', 'samsung s20', 'iphone 11 pro', 'iphone 8', 'rolex watch'}