Python sets
In Python, set is a collection of dissimilar objects separated by comma and are placed in curly braces '{}'.
- In set, same values are not repeated.
- No duplicate elements.
- All the elements are unique.
- If the values are repeated then, one value is kept and the remaining repeating values are discarded automatically.
- Sets are unordered.
- Sets are unindexed, because the elements gets shuffled after declaration (unordered).
- The existing elements in set can not be modified or changed.
- But can be removed.
- And new elements can be added in set.
Program
Initialization of set
There are 2 methods to initialize set.
Direct way of initializing a set
Objects are placed in curly braces'{}', for initialization of a set.
Program
An empty curly brackets represents Python dictionary
Program
Initialization with set() method
Collection of objects are given as an argument to the set() built-in function.
The collection of objects which are passed as an argument should be separated by comma and should be placed in brackets.
Program
For declaration of empty set, use the set() method.
Program
Accessing values in set
Accessing a value using index, is not possible in set. Because they are unordered.
The only way of accessing values in a set is, by using a for loop. LEARN MORE...
Adding elements in set
In sets, add() and update() are the two methods to add element/s.
Set add() method
An element is added to the set.
No effect if the element already exists.
Only one element is passed as an argument to the add() method, in set.
Program
Set update() method
More elements are added using this method, in set.
The elements to be added are declared in list or tuple and are passed as an argument to the update() method.
Program
>>> a = {1, 2, 3}
Removing element in set
For removing an element in set, pop(), remove() or discard() method is used.
Set pop() method
Remove and return arbitrary element in a set.
If the set is empty then, KeyError is raised.
Program
Set remove() method
Removes the specified element in a set.
If the element is not a member, then KeyError is raised.
Program
Set discard() method
Removes the specified element in a set.
If the element is not a member, does nothing.
Program
Set clear() method
This method makes a set empty.
Program
Copy a set
The command 'set1 = set2' will not work because, the changes made in set1 will automatically be made in set2. As both the sets will have the same memory location.
Program
Joining set
There are 2 methods to join sets.
- union() method
- update() method
Here, the '+' operator can not be used for joining the sets.
Set union() method
Creates a new set by joining 2 sets.
Program
Set update() method
With this method one set is inserted into the another set.
In this, new set is not created.
To create a new set by joining two sets use union() method.
Program()
Some more methods to learn...
Method | Meaning |
---|---|
difference() | Returns the difference of two sets. a.difference(b), common elements will be removed from 'a' with respect to 'b' and return the copy of remaining elements |
difference_update() | Removes the common elements a.difference_update(b), common elements will be removed from 'a' with respect to 'b'. |
intersection() | Returns a new set in which each element is present in the two or more sets specified. |
intersection_update() | Removes the elements from one set which are not present in another set |
isdisjoint() | Returns True if both the sets do not have any common element |
issubset() | a.issubset(b), returns True if all the elements of set 'a' are present in the set 'b' |
issuperset() | a.issuperset(b), returns True if 'a' contains all the elements of 'b' |
symmetric_difference() | Returns a new set in which all the elements are taken from both the sets, but the elements which are present in both sets are not included |
symmetric_difference_update() | a.symmetric_difference_update(b), Update set a by inserting elements of 'b' to 'a' and removing the elements which are present in both |
إرسال تعليق
If you have any query, feel free to ask.