Find the length of a set in Python Last Updated : 21 Feb, 2025 Comments Improve Suggest changes Like Article Like Report We are given a set and our task is to find its length. For example, if we have a = {1, 2, 3, 4, 5}, the length of the set should be 5.Using len()len() function in Python returns the number of elements in a set. It provides total count of unique items present in set. Python a = {1, 2, 3, 4, 5} # Returns the number of elements in the set l = len(a) print(l) Output5 Explanation:len(a) function returns the total number of unique elements in the set a, which is 5.Result is stored in the variable "l" and printed to display the length of set.Using a LoopUsing a loop to find the length of a set involves iterating through each element and incrementing a counter for every item. This manual method replicates the functionality of len(). Python a = {1, 2, 3, 4, 5} c = 0 for element in a: # Increment the count for each element c += 1 print(c) Output5 Explanation:Loop iterates over each element in set a incrementing counter "c" by 1 for every element.After loop completes "c" holds total number of elements in the set which is printed as length of set.Using sum()sum() function can calculate length of a set by summing 1 for each element in set using a generator expression. This method provides same result as len() through manual counting. Python a = {1, 2, 3, 4, 5} # Counts the number of elements l = sum(1 for _ in a) print(l) Output5 Explanation:Sum(1 for _ in a) expression iterates over each element in set "a" and adds 1 for each element.Total sum represents the number of elements in the set which is then printed as length of set.Using enumerate()enumerate() function can be used to iterate through a set with an index. By tracking last index during iteration, we can determine the length of set. Python a = {1, 2, 3, 4, 5} for index, _ in enumerate(a): pass # Since index starts from 0 l = index + 1 print(l) Output5 Explanation:enumerate(a) function iterates over each element in set a assigning an index to each element.Length is calculated as index + 1 because the index starts from 0 representing total number of elements in set. Comment More infoAdvertise with us Next Article Find the length of a set in Python N nikhilaggarwal3 Follow Improve Article Tags : Python Python Programs python-set Practice Tags : pythonpython-set Similar Reads Python Set Methods A Set in Python is a collection of unique elements which are unordered and mutable. Python provides various functions to work with Set. In this article, we will see a list of all the functions provided by Python to deal with Sets. Adding and Removing elementsWe can add and remove elements form the s 2 min read Set add() Method in Python The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot 4 min read Python Set clear() Method Python Set clear() method removes all elements from the set. Python Set clear() Method Syntax: Syntax: set.clear() parameters: The clear() method doesn't take any parameters.  Return: None Time complexity : The time complexity of set.clear() function on a set with n element is O(n) . Example 1: Pyth 2 min read set copy() in python The copy() method returns a shallow copy of the set in python. If we use "=" to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set. So we have to create a shallow copy of the set such that when we modify something in the copied set, change 2 min read Python Set discard() Function Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value. Example: In this example, we are removing the integer 3 from the set with discard() in Python. Python3 my_set = {1, 2, 3, 4, 5} my_set.discar 3 min read Python Set | difference_update() The difference_update() method helps in an in-place way of differentiating the set. The previously discussed set difference() helps to find out the difference between two sets and returns a new set with the difference value, but the difference_update() updates the existing caller set.If A and B are 1 min read Python Set difference() In Python, the difference() method is used to find elements that exist in one set but not in another. It returns a new set containing elements from the first set that are not present in the second set. This operation is similar to the subtraction of sets (A - B), where only unique elements from the 2 min read issuperset() in Python Python Set issuperset() method returns True if all elements of a set B are in set A. Then Set A is the superset of set B. Python issuperset() Method Syntax: Syntax: A.issuperset(B) Parameter: Any other Set to compare with Return: boolean value  Python issuperset() exampleExample 1: Working of issubs 1 min read Python Set issubset() Method Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python. Python Set issubset() Method SyntaxSyntax: set_obj.issubset(other_set) Parameter: other_set: any other set to 2 min read Python Set isdisjoint() Method Python set isdisjoint() function check whether the two sets are disjoint or not, if it is disjoint then it returns True otherwise it will return False. Two sets are said to be disjoint when their intersection is null. Python set isdisjoint() Method Syntax: Syntax: set1.isdisjoint(set2) Parameters: 2 min read Like