How to Use len() Function in Python

How to Use len() Function in Python

4 mins read326 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 30, 2023 12:27 IST

The len function in python that counts the number of items in an object. In this article, we will discuss len function in python and how to use it with list, tuple, array, string, range, and with sets.

2023_01_MicrosoftTeams-image-126.jpg

In Python, the len() function is essential and easy to use. It helps you find out how many items are in a data structure like a list, string, tuple, or dictionary. Simply put, len(your_object) tells you how many elements are in your_object, making it a fundamental tool for managing and navigating through data in Python.

Table of Content

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
3 months
– / –
2 weeks
– / –
16 weeks
β‚Ή1.7 K
3 months
– / –
– / –
β‚Ή4.24 K
2 weeks
β‚Ή3 K
3 weeks
– / –
4 months

What is the len() function in Python?

Definition

The len() function is one of the built-in functions in python that counts the number of items in an object (string, list, tuple, set, or sequence). It returns the number of objects in an integer format.

  • String: It will return the number of characteristics (including spaces).
  • List or Tuple: It will return the number of items (that are separated by commas, not the characteristics).

Syntax

The syntax for the length function in python is straightforward; you just have to give the variable name inside the len() function, i.e.,

 
len(object)
Copy code

Parameter

Object – Required – Must be a sequence or collection

Sequence: string, byte, tuple, list, etc.

Collection: dictionary, set

What is Programming What is Python
What is Data Science What is Machine Learning

Until now, we have a clear understanding of the len function in python; now, we will take some examples to understand better how it is used.

Examples

Example-1: Len() function with List

 
#find the length of the list
#define list
color = ['blue', 'green', 'red', 'orange', 'yellow']
len(color)
Copy code

Output

2023_01_image-54.jpg

Must Read: Python List

Example – 2: Len() function with Tuple

 
#find the length of the tuple
#define tuple
bike = ('yamaha', 'honda', 'tvs', 'RE')
len(bike)
Copy code
#find the length of the string
#define string
company = ('we are shiksha online')
len(company)
Copy code
#find the length of the range
#use range function
r = range(1, 100)
len(r)
Copy code

Output

2023_01_image-58.jpg

Must Read: Python Range

Example – 5: Len() function with Dictionaries

 
#find the length of the dictionary
#define a dictionary
roll_no = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
len(roll_no)
Copy code

Output

2023_01_image-59.jpg

Must Read: Python Dictionary

Example – 6: Len() function with Sets

 
#find the length of the set
#define a set
car = {'tata', 'hyundai', 'skoda'}
len(car)
Copy code

Output

2023_01_image-60.jpg

Must Read: Python Sets

Conclusion

The len() function in python is used to count the number of items in that object. In this article, we have learned how to use python len with the help of several examples.

Hope you will like the article.

Top Trending Article

Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python

Interview Questions

Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |

FAQs

What is the len() function in Python and how is it used?

The len() function in Python is a built-in function that returns the length of an object, such as the number of items in a list, characters in a string, or elements in a tuple. It's used by simply calling len(object), where an object can be a sequence (like a string, list, tuple) or a collection (like a dictionary, set, or frozen set).

Can len() be used with user-defined classes?

Yes, len() can be used with user-defined classes. You need to implement the .__len__() method in your class definition. When len() is called with an instance of your class as its argument, Python internally calls the .__len__() method of that instance to determine its length.

Are there any data types with which len() cannot be used?

Yes, len() cannot be used with all data types. For instance, it raises a TypeError when used with data types that don't inherently support a concept of length, such as integers, floats, and Boolean types. len() also cannot be used directly with iterators and generators, as their lengths aren't predetermined and can be infinite.

How does len() work with third-party data types like NumPy arrays and pandas DataFrames?

When used with a NumPy array, len() returns the size of the first dimension. For a pandas DataFrame, it returns the number of rows. The behavior of len() with these data types depends on how their respective classes have implemented the .__len__() method.

What happens if len() is used on an empty object?

If len() is used on an empty object like an empty list, string, or tuple, it returns 0. This is because there are no elements in the object to count.

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio