Open In App

Find the length of a set in Python

Last Updated : 07 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, a Set is a collection data type that is unordered and mutable. A set cannot have duplicate elements. Here, the task is to find out the number of elements present in a set. See the below examples.

Examples:

Input: a = {1, 2, 3, 4, 5, 6}
Output: 6

Input: a = {'Geeks', 'For'}
Output: 2

The idea is use len() in Python

Find-Length-of-set

Example 1:

Python3




# Python program to find the length
# of set
  
set1 = set()  
    
# Adding element and tuple to the Set 
set1.add(8
set1.add(9
set1.add((6, 7)) 
  
print("The length of set is:", len(set1))


Output:

The length of set is: 3

Example 2:

Python3




n = len({1, 2, 3, 4, 5})
  
print("The length of set is:", n)


Output:

The length of set is: 5

How does len() work?
len() works in O(1) time as the set is an object and has a member to store its size. Below is description of len() from Python docs.

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).


Previous Article
Next Article

Similar Reads

Python string length | len() function to find string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method. Example: C/C++ Code string = "Geeksforgeeks" print(len(string)) Output13String len() Syntax len(string) Parameter String: string of which you want to find the length. Return: It returns
3 min read
How to Fix: Length of values does not match length of index
In this article we will fix the error: The length of values does not match the length of the index in Python. Cases of this error occurrence: C/C++ Code # importing pandas import pandas as pd sepal_length = [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9] sepal_width = [4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.1, 5.2, 5.3] petal_length = [3.3, 4.6, 4
2 min read
Find length of a string in python (6 ways)
Strings in Python are immutable sequences of Unicode code points. Given a string, we need to find its length. Examples: Input : 'abc' Output : 3 Input : 'hello world !' Output : 13 Input : ' h e l l o ' Output :14 Methods#1: Using the built-in function len. The built-in function len returns the number of items in a container. C/C++ Code # Python co
3 min read
Python | Find maximum length sub-list in a nested list
Given a list of lists, write a Python program to find the list with maximum length. The output should be in the form (list, list_length). Examples: Input : [['A'], ['A', 'B'], ['A', 'B', 'C']] Output : (['A', 'B', 'C'], 3) Input : [[1, 2, 3, 9, 4], [5], [3, 8], [2]] Output : ([1, 2, 3, 9, 4], 5) Let's discuss different approaches to solve this prob
3 min read
Python | Find whether all tuple have same length
Given a list of tuples, the task is to find whether all tuple have same length. Below are some ways to achieve the above task. Method #1: Using Iteration C/C++ Code # Python code to find whether all # tuple have equal length # Input List initialization Input = [(11, 22, 33), (44, 55, 66)] # printing print("Initial list of tuple", Input) #
6 min read
Python - Find the sum of Length of Strings at given indices
Given the String list, write a Python program to compute sum of lengths of custom indices of list. Examples: Input : test_list = ["gfg", "is", "best", "for", "geeks"], idx_list = [0, 1, 4] Output : 10 Explanation : 3 + 2 + 5 = 10. (Sizes of strings at idx.) Input : test_list = ["gfg", "is", "best", "for", "geeks"], idx_list = [0, 2, 4] Output : 12
4 min read
Python Program to find all Palindromic Bitlists in length
In this article, we will learn to generate all Palindromic Bitlists of a given length using Backtracking in Python. Backtracking can be defined as a general algorithmic technique that considers searching every possible combination in order to solve a computational problem. Whereas, Bitlists are lists of bits(0 and 1). A bit list that is a palindrom
6 min read
Python Program To Find Length Of The Longest Substring Without Repeating Characters
Given a string str, find the length of the longest substring without repeating characters.  For “ABDEFGABEF”, the longest substring are “BDEFGA” and "DEFGAB", with length 6.For “BBBB” the longest substring is “B”, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below diagrams, with length 7 The desired time complexi
6 min read
How To Find the Length of a List in Python
List being an integral part of Python programming has to be learned by all Python users and having a knowledge of its utility and operations is essential and always a plus. Many operations are performed in lists, but in this article, we will discuss the length of a list. The length of a list means the number of elements it has. We are going to look
9 min read
Find the Length of a String Without Using len Function in Python
In this program, we are given a string and we have to find out the length of a string without using the len() function in Python. In this article, we will see how we can find the length of a string without using len() function in Python. Example : Input: 'abc' Output : 3 Explanation: Length of 'abc' is 3 Python Program to Calculate the Length of a
3 min read
Practice Tags :