Python Sets are another datatype in python which are unordered and eliminates duplication of elements. Items are put one after another enclosed by curly brackets.
Creating empty Sets
- By using curly brackets
{}
dictionary data-type. - Also python built-in constructor for sets
set()
is available.
Important Points about Python Sets:
1) They are an unordered and unindexed collection of datatypes.
2) Duplicate elements are not allowed in sets.
3) Unlike a list, tuples, sets cannot be accessed through index or key.
4) items in the sets are not in sequence they usually change their sequence.
Example: Create an empty Sets
create sets using curly brackets
a={}
create sets using python built-in set constructor
b=set()
print(a)
print(b)
#PYTHON OUTPUT
{}
set()
Example: Initialize sets using predefined variables.
create sets using curly brackets
a={
'sets',
'change',
'their',
'sequence'
}
create sets using python built-in set constructor
b=set((
1,2,3
))
print(a)
print(b)
#PYTHON OUTPUT
{'sequence', 'sets', 'their', 'change'}
{1, 2, 3}
In the above example, we can see that in sets the sequences(occurrences) of variables change.
Example: Sets Eliminate duplicate items
a={
1,2,3,3,2,1
}
print(a)#note that duplicate elements has been ommited by sets
#PYTHON OUTPUT
{1, 2, 3}
Accessing Sets
Items inside sets cannot be accessed using the index. To access it we have to loop sets using a for-in loop
Example
a={
'alice_586',
'Alice',
'K',
'123456',
}
for i in a:
print(i)
#PYTHON OUTPUT
K
123456
Alice
alice_586
To verify if a given item exists in Sets
a={
'alice_586',
'Alice',
'K',
'123456',
}
print('Alice' in a)# returns True
print('Anthony' in a)#returns False
#PYTHON OUTPUT
True
False
To count the number of items present in sets use len()
method.
a = {1,2,3,4,}
print(len(a))
#PYTHON OUTPUT
4
To add new items in sets use add()
method under sets. But you can add only one value at a time.
new_set = {'BMW', 'Volvo'}
new_set.add("Jaguar")
print(new_set)
#PYTHON OUTPUT
{'BMW', 'Volvo', 'Jaguar'}
To add multiple items in sets using update()
method under sets.
Syntax
sets_name.update(another sets variable)
Example
old_set = {'BMW', 'Volvo'}
new_set = {'Porsche', 'Bugatti', 'Pagani'}
old_set.update(new_set)
print(old_set)
#PYTHON OUTPUT
{'Bugatti', 'Volvo', 'BMW', 'Porsche', 'Pagani'}
Note : You cannot update the existing item in sets.
To remove existing item in sets use 'remove()' or 'discard()' method
new_set = {'Asia', 'Africa', 'Europe'}
new_set.remove('Asia')
print(new_set)
#PYTHON OUTPUT
{'Europe', 'Africa'}
Note: While using 'remove()' method we must know that if the item doesn't exists. It will give 'KeyError' Error.
Insisted ofremove()
usediscard()
method.
new_set = {'Asia', 'Africa', 'Europe'}
new_set.remove('Asa')
print(new_set)
#PYTHON ERROR OUTPUT
KeyError: 'Asa'
Using discard()
method
new_set = {'Asia', 'Africa', 'Europe'}
new_set.discard('Asia')
print(new_set)
#PYTHON OUTPUT
{'Africa', 'Europe'}
If the item does not exists.
new_set = {'Asia', 'Africa', 'Europe'}
new_set.discard('Asi')
print(new_set)
#PYTHON OUTPUT
{'Europe', 'Asia', 'Africa'}
To clear items inside sets use clear()
method under sets.
old_set = {'BMW', 'Volvo'}
new_set = {'Porsche', 'Bugatti', 'Pagani'}
old_set.update(new_set)
old_set.clear()
print(old_set)
#PYTHON OUTPUT
{}
The article is taken from the code learners python sets post
Top comments (0)