PYTHON SET

Set: Set is a data structure in python. We can call it as a container which store the items in curly braces {} separated by comma.  set is unordered. The main purpose of a set is no duplicate values.

Define Set

Define a set: either with set() or with {} 

animals = {'deer','tiger','lion','fox'}
                  or
animals = set(('deer','tiger','lion','fox'))

We may or may not get output in same order as we define

Print a set

animals = set(('deer','tiger','lion','fox'))
print(animals)

Output: {'lion', 'tiger', 'deer', 'fox'}

Note : We may or may not get output in same order as we define

If we provide one string in set() it will convert each letter as each item and store. In the following example, there are 2 e’s but in output only 1'e 

name=set('peer') 
print(name) 

Output: {'e', 'p', 'r'} 

If items are repeated in the set, it will remove the repeated items, see the following code and it's output understand

animals = set(('deer','tiger','lion','tiger','fox'))
print(animals)

Output: {'fox', 'lion', 'deer', 'tiger'}

Note : In the above example we have two tiger’s but we got only one tiger in the output, because repeated items will remove by the set.

Length of Set

len() is used to find the length

animals={'cat', 'dog', 'cow', 'lion', 'cat'}
print(len(animals)) 

Output: 4

Note : If you observe there are 5 items in the animals set but output is 4. Because set will not consider repeated items. it will count only distinct items.

Add element to set

add() method is used to add elements or items to the set.

animals={'cat', 'dog', 'cow'}

animals.add('tiger')

print(animals)

Output: {'cow', 'dog', 'tiger', 'cat'}

If we want add more than one item to the set, we can use update()

animals={'cat', 'dog', 'cow'}

animals.update(('tiger','lion'))

print(animals)

Output: {'cow', 'cat', 'dog', 'lion', 'tiger'}

Remove an element from the set

With the help of remove() and discard() we can remove specified item from the set 

remove() method

animals={'cat', 'dog', 'cow', 'tiger'}

animals.remove('cow')

print(animals)

Output: {'tiger', 'cat', 'dog'}

discard() method

animals={'cat', 'dog', 'cow', 'tiger'} 
animals.discard('tiger') 
print(animals) 

Output: {'cat', 'cow', 'dog'} 

pop() method

We can also use pop() to remove item from set. pop() will remove last item but set is unordered so we cannot predict which item it will remove from set. 

animals={'cat', 'dog', 'cow', 'tiger'} 
animals.pop() 
print(animals)

Output: {'dog', 'tiger', 'cow'} 

clear() method

clear() method will delete all elements in the set

animals={'cat', 'dog', 'cow', 'tiger'} 
animals.clear() 
print(animals) 

Output: set()

del 

del will delete permanently

animals={'cat', 'dog', 'cow', 'tiger'} 
del animals 
print(animals) 

Output: NameError: name 'animals' is not defined  #because we deleted the set before printing 

Set Operations

We can perform the same mathematical operations like union, intersection, difference and symmetric on set

union

union will combine all elements from two sets. No duplicates.

animals={'cat', 'dog', 'cow', 'lion', 'tiger'} 
wild_animals = {'lion', 'tiger','wolf','rhino'} 
print(animals.union(wild_animals)) 

Output: {'dog', 'tiger', 'rhino', 'wolf', 'cat', 'cow', 'lion'} 

intersection

intersection will give elements which are there in both sets.

animals={'cat', 'dog', 'cow', 'lion', 'tiger'} 
wild_animals = {'lion', 'tiger','wolf','rhino'} 
print(animals.intersection(wild_animals)) 

Output: {'tiger', 'lion'}

'tiger' and 'lion' are in both sets(animals, wild_animals)

difference

Here we are comparing one set with another. If that set has different elements from another set, it will give only those elements.

animals={'cat', 'dog', 'cow', 'lion', 'tiger'} 
wild_animals = {'lion', 'tiger','wolf','rhino'} 
print(animals.difference(wild_animals)) 

Output: {'cow', 'cat', 'dog'}

symmetric_difference

If we want all the elements from two sets except the elements which are common in those two sets, we can use symmetric_difference

animals={'cat', 'dog', 'cow', 'lion', 'tiger'} 
wild_animals = {'lion', 'tiger','wolf','rhino'} 
print(animals.symmetric_difference(wild_animals))

Output: {'dog', 'rhino', 'cow', 'cat', 'wolf'}

 Contact Us:

                  codecalculatenr@gmail.com

 

About Us : 

        Code Calculate is the biggest online platform