Python len() Practical Examples

Introduction

The len() function is used to find the length of a set in Python. This function is a built-in function that comes along with the installation of Python.

Python has some very useful built-in data types like lists, tuples, dictionaries, and sets. The goal of this article is to familiarize you with the len() function so we will see how we can find the length of sets, tuples and dictionaries using len(). Let’s start.

Advertisement

 

Python implementation of len()

Python has some very useful built-in data types like lists, tuples, dictionaries, and sets. Sets are among these data types which are used to store multiple items in a variable. len() can also be used to find the length of tuples, etc.

 

Using len() with sets

A Set in Python is an unordered, mutable collection data type. Duplicate items cannot exist in a set. Discovering how many components are included in a set is the problem at hand. To see how many elements are there in a set, we use the len() function which will tell us the length of the set. A set can be defined in Python as shown below.

Set = {10,20,30,40,50}
Set2 = {'Apple','Mango','Banana'}

After we define a set, if we want to see the length of the defined set, we use the len() function. In the following code example, we are checking the length of the above sets.

Set1 = {10,20,30,40,50}
Set2 = {'Apple','Mango','Banana'}
print('length of set1', len(Set1))
print('length of set2', len(Set2))

Output:

length of set1 5
length of set2 3

 

Using len() with tuple

A tuple can be defined in python using () braces. The Tuples is also the collection of elements but we cannot modify elements inside a tuple. We can use the len() to find the length of the tuple. Let’s see an example.

tuple1, tuple2 = (1,2,3,4,5), ('Apple', 'Mango', 'Banana')
 
print(len(tuple1))
print(len(tuple2))

Output:

Advertisement
5
3

We have initialized two tuples tuple1, and tuple2. The first tuple has integers values and the second one is having string values. The Python len() function can also be used with tuples to see the number of elements in tuples. We passed our tuples to the len() function and got the length of the tuples.

 

Using len() with dict

A dictionary is another data type that stores elements in an unordered format. Dictionaries in python are mutable. We initialize dictionaries using curly braces and they store key-pair values which are separated by commas.

We can use the len() function with dictionaries also. Let’s see an example to understand it better.

dict1 = {'Name' : 'Nisar', 'GPA' : 3.6, 'Profession' : 'Data scientist'}
print(len(dict1))

Output:

3

We initialized a python dictionary with key-value pairs, and we have passed our dictionary to the len() function which will calculate the number of elements in the dictionary. The key and value are considered as one value in python. That’s all for today. See you soon.

Thank you!

Advertisement

 

Summary

Today, we saw how the len() function can be used with python for different data types like sets, tuples, and dictionaries. We have seen the len() with different examples.

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X