Python Program for Set len() Method

In the previous article, we have discussed Python Program for Set sorted() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}

Output:

The given set is :
{'is', 'btechgeeks', 'hello', 'this'}
The given set's length =  4

Example2:

Input:

Given set = {34, 2, 56, 12, 1, 4, 2, 5}

Output:

The given set is :
{1, 34, 2, 4, 5, 12, 56}
The given set's length = 7

Explanation:

Here the total items present in the set is 8, but set doesn't allow duplicate elements. 
Here 2 is the duplicate element. so, it gives the length of set as 7.

Program for Set len() Method in Python

set len() Method:

The set len() method is one of the set methods for determining the length of a set i.e, the total number of items present in the set.

Syntax:

len(set)

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply len() method to the given set to get the total number of items present (length) in the given set.
  • Store it in another variable.
  • Print the length of the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply len() method to the given set to get the total number of items present
# (length) in the given set.
# Store it in another variable.
gvn_setlengt = len(gven_set)
# Print the length of the given set.
print("The given set's length = ", gvn_setlengt)

Output:

The given set is :
{'is', 'btechgeeks', 'hello', 'this'}
The given set's length =  4

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply len() method to the given set to get the total number of items present (length) in the given set.
  • Store it in another variable.
  • Print the length of the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply len() method to the given set to get the total number of items present
# (length) in the given set.
# Store it in another variable.
gvn_setlengt = len(gven_set)
# Print the length of the given set.
print("The given set's length = ", gvn_setlengt)

Output:

Enter some random values separated by spaces = 34 2 56 12 1 4 2 5
The given set is :
{1, 34, 2, 4, 5, 12, 56}
The given set's length = 7

Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples tutorial will definitely aid programmers to learn easily & efficiently.