# Find the length of a set in Python

## Find the length of a set in Python

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
```

Thee idea is use len() in Python

Example 1:

 `# 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:

 `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).

Last Updated on March 1, 2022 by admin

## Python – How to search for a string in text files?Python – How to search for a string in text files?

Python – How to search for a string in text files? In this article, we

## Django Project MVT StructureDjango Project MVT Structure

Django Project MVT Structure Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern

## Python Program to convert List of Integer to List of StringPython Program to convert List of Integer to List of String

Python Program to convert List of Integer to List of String Given a List of

## Python bytearray() functionPython bytearray() function

Python | bytearray() function bytearray() method returns a bytearray object which is an array of given

## Beautifulsoup Installation – PythonBeautifulsoup Installation – Python

Beautifulsoup Installation – Python Beautiful Soup is a Python library for pulling data out of

## Randomly select n elements from list in PythonRandomly select n elements from list in Python

Randomly select n elements from list in Python In this article, we will discuss how

## Create XML Documents using PythonCreate XML Documents using Python

Create XML Documents using Python Extensible Markup Language(XML), is a markup language that you can

## First Class functions in PythonFirst Class functions in Python

First Class functions in Python First class objects in a language are handled uniformly throughout. They