To get the length of a set in Python, you can use the len
function.
In this example,
my_set
and initialize it with some string elements.len
function to get the number of elements in the set.print
function.my_set = {'apple', 'banana', 'orange'}
# Get the length of the set
length = len(my_set)
# Print the length of the set
print(f'Length of the set: {length}')
Length of the set: 3
In this example,
empty_set
using the set
constructor.len
function to get the number of elements in the set.print
function.empty_set = set()
# Get the length of the empty set
length = len(empty_set)
# Print the length of the empty set
print(f'Length of the empty set: {length}')
Length of the empty set: 0
In this tutorial, we learned How to Get Length of a Set in Python language with well detailed examples.