Set, a mathematical term for a sequence consisting of unique languages is also extended in Python’s language and may be readily created using a Python set(). We will look at Python set(), different Python set methods in this article.
Python Set
A set is a group of elements that are not in any particular sequence. Every set element must be unique (no duplication) and immutable (cannot be changed).
A set, on the other hand, is mutable. We have the ability to add and remove items from it. Sets can also be utilized to conduct mathematical set operations such as union, intersection, and symmetric difference, among others.
Creating Python Sets
In Python, a set is created by placing all the elements/items inside the curly braces {}, each value separated by a comma, or by using the Python set() built-in function.
The set can contain any number of items that may be of different data types such as integer, float, tuple, string. But it cannot contain mutable elements such as lists, dictionary’s and sets themselves.
Example
# Python program to create a set
# using curly braces
my_set = {1, 2, 3, 4, 5}
print('Created set:', my_set)
# using set() function
names = ['Ricky', 'Daisy', 'Lily', 'John']
# creates set from list
names_set = set(names)
print('Created set:', names_set)
Output
Created set: {1, 2, 3, 4, 5}
Created set: {'Lily', 'Daisy', 'Ricky', 'John'}
Python Set Methods
Just like Python string, list, and dictionary class methods, the set() function also has different methods that are used for performing various operations on the data elements. Let us look at the Python set methods provided by the Python programming language.
Method | Description |
Python frozenset() | Returns an immutable frozenset object |
Python Set add() | Adds elements to the set |
Python Set clear() | Deletes all the elements from the set |
Python Set copy() | Returns a copy of the set |
Python Set difference() | Returns the difference between 2 or more sets |
Python Set difference_update() | Updates the calling set by removing the items that are also included in the other specified sets |
Python Set discard() | Removes a specific element from the set |
Python Set intersection() | Returns an intersection of 2 or more sets |
Python Set intersection_update() | Updates the calling set by removing the items that are not present in the other specified sets |
Python Set isdisjoint() | Returns whether 2 sets have an intersection or not |
Python Set issubset() | Returns whether one set contains this set or not |
Python Set issuperset() | Returns whether this set contains another set or not |
Python Set pop() | Removes an arbitrary element |
Python Set remove() | Removes the specified element from the set |
Python Set symmetric_difference() | Returns symmetric differences of 2 or more sets |
Python Set symmetric_difference_update() | Updates the Set with symmetric difference |
Python Set union() | Returns union of sets |
Python Set update() | Adds elements to the set |
Frequently Asked Questions
Q1. What is a set method in Python?
A set is a group of elements that are not in any particular sequence. Every element inside the set must be unique (no duplication) and immutable (cannot be changed). The set() method is used to transform any iterable to a sequence of iterable items with separate elements, often known as a Set.
Q2. How do you use the set method in Python?
In Python, you can define a Set by placing all the elements/items inside the curly braces {}, each value separated by a comma, or by using the Python set() built-in function.
Example
# Python program to create a set
# using curly braces
my_set = {'a', 'b', 'c'}
print('Created set:', my_set)
# using set() function
nums = ['I', 'II', 'III', 'IV']
# creates set from list
names_set = set(nums)
print('Created set:', names_set)
Output
Created set: {'c', 'a', 'b'}
Created set: {'IV', 'II', 'I', 'III'}
Q3. What is a Hashset and Frozenset in Python?
- Hashset – The HashSet class is used to create a collection that stores data in a hash table. It derives from AbstractSet and implements the Set interface. HashSet uses a process known as hashing to store the elements. Only unique elements are stored in HashSet. HashSet accepts null values. The insertion order is not preserved by HashSet. Elements are put here based on their hashcode.
- Frozenset – In Python, a frozenset is the same as a set, except that frozensets are immutable, which implies that once generated, elements from the frozenset cannot be added or removed. This function accepts any iterable object as input and turns it into an immutable object. It is not assured that the order of the elements will be retained.
Leave a Reply