Sets in Python | Python Programming
Sets in Python: Set in python is another type of data type which is represented by curly braces {} same as the dictionary. Sets are un-ordered and un-indexed data type. However, Set itself is mutable. We can add or remove items from sets. Sets can also be used to perform mathematical set operations like union, intersection, etc. In Sets, we are not sure in which order the items will appear.
How to create Set: A Set in python can be defined by {} curly braces. It can contain any number of items of different data types i.e. (integer, float, tuple etc.). But it cannot contains mutable data types i.e. a list, dictionary etc.
Syntax:
#Sets Syntax test = {"papaya", "guava", "oranges"} print(test) # output # {'oranges', 'guava', 'papaya'}
Note: A Set cannot contain duplicate items. However, Set is mutable means we can add or remove data from a Set.
Operations on Sets:
Below are some available set methods in python, that are applicable on sets:
1). Accessing Elements: As already mentioned above that sets are un-indexed data type, means its values doesn’t have an index. So, it is not possible to access the item inside sets using indexing. But we can iterate over the set items using for loop. Also, we can use a membership operator along with for loop.
Example:
#Sets example test = {"guava", "mango", "papaya"} for data in test: print(i) # output # guava # mango # papaya
2). Updating or Changing items: Once the set has been created, then we cannot change its values or element. But we can add new elements/values to them. This means we can add or remove items to set but do not update the existing elements of set. We have two methods that are used to add more items to our set.
- add(): This method is used to add one item to set.
- update(): update() set methods in python is used to add more than one item to set.
3). Remove or Delete: There are two methods available in python to remove elements from sets. These two methods are:
- remove()
- discard()
4). PoP() Method: This method is used to remove the last element from a set. As the sets are an unordered set of data types, so we do not know, when we use the pop() method, which element is going to remove. It is random data removal.
Example:
#Pop method in Sets() test = {"guava", "papaya", "berry"} test.pop() print(test) # output # {'guava', 'papaya'}
5). Del keyword: The del keyword of Python is used to delete the complete set.
Example:
#Del method in Sets() test = {"guava", "papaya", "berry"} print(test) # output # {'guava', 'papaya'} # Deleting Set del test print(test) # output # Traceback (most recent call last): File "./prog.py", # line 10, in NameError: name 'test' is not defined
6). Joining two sets: We can also join two or more sets using available sets methods. There are two types of methods that are used to join sets:
1). Set Union(): The meaning of union in mathematic is to get all the values of the given sets. Suppose set1 and set2 have been given and we have to perform the operation of union on both sets. Follow below given example:
Example 1: Using union() method.
#union() in sets set1 = {"c", "b" , "a",1} set2 = {3, 2, 1,1} test = set1.union(set2) print(test) # output # {1, 2, 3, 'c', 'a', 'b'}
Example 2: Using |operator.
#using | operator set1 = {"c", "b" , "a",1} set2 = {3, 2, 1,1} print(set1 | set2) # output # {1, 2, 3, 'c', 'a', 'b'}
2). Set Intersection(): The meaning of intersection in mathematics is to get only those values that are common in the given sets. Suppose set1 and set2 have been given and we have to perform the operation of intersection on both sets. The intersection of set1 and set2 is a set of elements that are common in both sets. Follow below given example:
# Intersection of sets set1 = {3, 2, 1, 5, 6} set2 = {2, 6, 5, 3, 3} print(A & B) # use & operator # output: # {4, 5}
7). Constructor set(): A constructor is also available in sets to create sets explicitly:
Example:
#Set() Constructor test = set(("mango", "guava", "pears")) print(test) # output # {'pears', 'mango', 'guava'}
8). Clear() method: Clear() method in python is used to clear the elements of the set completely.
Example:
#clear method test = {"car", "truck", "bus"} test.clear() print(test) # output # {}
Summary of available set methods in python
We already learnt about available set methods in python. Below is the summary given for a quick overview of available set methods in python.
Method Name | Description |
copy() | It returns a copy of the set. |
clear() | It removes all the elements from the set. |
discard() | It removes the specified or selected item from the set. |
add() | It is used to add an element to the set. |
pop() | Removes an element from the set. |
intersection() | It returns a set(), which is the intersection of two sets. |
remove() | It is used to remove the specified element from the set. |
union() | It returns a set, which contains the union of sets. |
update() | It updates the set with the union this set and another set. |