Set Data Type in Python
Set
Sets in Python are an unordered collection of different data types(immutable).Sets have no duplicate elements. The items in set are separated by commas (,) and enclosed within curly braces {}.
- Sets are mutable objects.
- Sets are iterable objects.
- Like a dictionary, set elements are non-subscriptable.
- Set does not support the duplicate value .
Data Visualization
Create empty set:
Since empty curly braces denote a dictionary hence to create empty set call set().
Python 3.8.4 (tags/v3.8.2:7bccb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = set() >>> print(data) set() >>> print(type(data))>>> print(len(data)) 0
Create non-empty set:
Python 3.8.4 (tags/v3.8.2:7b3cc59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>>data = {101,'ankita',95.6,'P','C','M'} >>>print(data) {101, 'ankita', 95.6, 'P', 'C', 'M'} >>> print(type(data))>>> print(len(data)) 6
Set does not support indexing so this reason we cannot view any particular data and slicing method. If you are try to print any particular value then raise following error,
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a={1,2,4,34,2,42,34,23,42,34,234,2,342,34} >>> a[3] Traceback (most recent call last): File "", line 1, in TypeError: 'set' object is not subscriptable >>> a[2] Traceback (most recent call last): File " ", line 1, in TypeError: 'set' object is not subscriptable >>> a[:2] Traceback (most recent call last): File " ", line 1, in TypeError: 'set' object is not subscriptable >>>
Data Insertion
In the heading will be learning about data insertion in the set, there python gives the some in-built function which is used to insert data in a new or existing set, Set insert the data at random palace.
Set Insertion Method
Function | Description |
add() | It is used for adding the single element in Set. |
update() | It is used to add multiple values in Set. |
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> set_use={"Ankit","Salesforcedrillers",12} >>> set_use.add("Noida") >>> print(set_use) {'Salesforcedrillers', 12, 'Ankit', 'Noida'} >>> set_use.add(1000) >>> print(set_use) {1000, 12, 'Ankit', 'Noida', 'Salesforcedrillers'} >>> set_use.update(["Multiple","Data","Insert"]) >>> print(set_use) {'Data', 1000, 12, 'Ankit', 'Noida', 'Salesforcedrillers', 'Multiple', 'Insert'} >>>
Data Deletion
We can delete the data in set using the following in-built function in set.
Set Deletion Method
Function | Description |
del set_name | It is delete all the list along with the element |
clear() | It deletes all the elements but lists exist. |
pop() |
|
remove(element) |
|
discard(element) |
|
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a={1,2,3,4,5,6,7,8,9,6,356,56,356,56,636,65,6,6346,3456,5,6345,65,6} >>> print(a) {3456, 1, 2, 3, 4, 5, 6, 7, 8, 9, 356, 65, 6346, 6345, 56, 636} >>> a.remove(2) >>> a.remove(3) >>> print(a) {3456, 1, 4, 5, 6, 7, 8, 9, 356, 65, 6346, 6345, 56, 636} >>> a {3456, 1, 4, 5, 6, 7, 8, 9, 356, 65, 6346, 6345, 56, 636} >>> a.remove(3) Traceback (most recent call last): File "", line 1, in KeyError: 3 >>> a.discard(1) >>> print(a) {3456, 4, 5, 6, 7, 8, 9, 356, 65, 6346, 6345, 56, 636} >>> a.discard(4) >>> print(a) {3456, 5, 6, 7, 8, 9, 356, 65, 6346, 6345, 56, 636} >>> a.discard(1) >>> a.discard(1) >>> a.pop() 3456 >>> print(a) {5, 6, 7, 8, 9, 356, 65, 6346, 6345, 56, 636} >>> a.pop() 5 >>> print(a) {6, 7, 8, 9, 356, 65, 6346, 6345, 56, 636} >>> a.clear() >>> print(a ... ) set() >>> del a >>> print(a) Traceback (most recent call last): File " ", line 1, in NameError: name 'a' is not defined >>>
Data updates are not allowed in set due to lack of index value.
Set Additional Feature
Different operations of the set,here we see the union,intersection,etc . Let’s take the program.
Function | Description |
s1.union(s2) | Find the union between s1 and s2. (i.e. all elements that are in either set s1 or s2.) |
s1.intersection(s2) | Find the intersection between s1 and s2 (i.e. all elements that are in both sets s1 and s2.) |
s1.difference(s2) | Find the difference between s1 and s2. (i.e. all elements that are in set s1 but not in s2.) |
s1.symmetric_difference(s2) | Find the symmetric_difference between s1 and s2. (i.e. all elements that are in exactly one of the sets s1 and s2.) |
Finding Union
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> s1={1,2,3,4,5,6} >>> s2={5,6,7,8,9,10} >>> s1.union(s2) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} >>> s2.union(s1) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} >>> print(s1&s2) {5, 6} >>> print(s1|s2) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} >>> print(s2|s1) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} >>>
Finding Intersection
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> s1={1,2,3,4,5,6} >>> s2={5,6,7,8,9,10} >>> s1.intersection(s2) {5, 6} >>> s2.intersection(s1) {5, 6} >>> print(s1&s2) {5, 6} >>> print(s2&s1 ... ) {5, 6} >>>
Finding Difference
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> s1={1,2,3,4,5,6} >>> s2={5,6,7,8,9,10} >>> s1.difference(s2) {1, 2, 3, 4} >>> s2.difference(s1) {8, 9, 10, 7} >>> print(s1-s2) {1, 2, 3, 4} >>> print(s2-s1) {8, 9, 10, 7} >>>
Finding Symmetric Difference
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> s1={1,2,3,4,5,6} >>> s2={5,6,7,8,9,10} >>> s1.symmetric_difference(s2) {1, 2, 3, 4, 7, 8, 9, 10} >>> s2.symmetric_difference(s1) {1, 2, 3, 4, 7, 8, 9, 10} >>> print(s1^s2) {1, 2, 3, 4, 7, 8, 9, 10} >>> print(s2^s1) {1, 2, 3, 4, 7, 8, 9, 10} >>>