What does the LEN () list method do?

Python List len[] Method

Advertisements


Previous Page

Next Page

Python len[]

In this tutorial, we will learn about the Python len[] function with the help of examples.

The len[] function returns the number of items [length] in an object.

Example

languages = ['Python', 'Java', 'JavaScript']

# compute the length of languages length = len[languages]

print[length] # Output: 3

List in Python

A list in Python is implemented to store the sequence of various types of data. However, there are six data types in Python that are capable of storing the sequences but the most common and reliable type is a list. To learn more about python you can join our Master Python programming course.

A list is defined as a collection of values or items of different types. The items in the list are separated with a comma [,] and enclosed with the square brackets [].

It is defined as follows:

list1 = ['edureka', 'python', 2019]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"];

If you want to learn Artificial Intelligence and Machine Learning in-depth, come to us and sign up for this Post Graduate Diploma AI and ML courses.

Python len[] Function

❮ Built-in Functions

Example

Return the number of items in a list:

mylist = ["apple", "banana", "cherry"]
x = len[mylist]

Try it Yourself »

Using the len[] Function in Python

by Stephen Gruppetta Oct 20, 2021 basics python

Mark as Completed

Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python's len[] Function

In many situations, you’ll need to find the number of items stored in a data structure. Python’s built-in function len[] is the tool that will help you with this task.

There are some cases in which the use of len[] is straightforward. However, there are other times when you’ll need to understand how this function works in more detail and how to apply it to different data types.

In this tutorial, you’ll learn how to:

  • Find the length of built-in data types using len[]
  • Use len[] with third-party data types
  • Provide support for len[] with user-defined classes

By the end of this article, you’ll know when to use the len[] Python function and how to use it effectively. You’ll know which built-in data types are valid arguments for len[] and which ones you can’t use. You’ll also understand how to use len[] with third-party types, such as ndarray in NumPy and DataFrame in pandas, and with your own classes.

Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.

Python len[] Method

The len[] function returns the length of the object. It returns total elements in an iterable or the number of chars in a string.

Syntax:

len[object]

Parameters:

object: Required. An iterable or string.

Return Value:

Returns an integer value indicating total elements or chars.

The following example demonstrates the len[] method.

Example: len[]

Copy

print["Total Elements in list: ", len[[1,2,3,4,5]]] print["Total Elements in tuple: ",len[[1,2,3,4,5]]] print["Total Elements in set: ",len[{1,2,3,4,5}]] print["Total Elements in dict: ",len[{1:'one',2:'two',3:'three',4:'four',5:'five'}]] print["string length : ", len["Hello World"]] print["byte length: ",len[b"12345"]] print["range[] length: ",len[range[0,10]]]

Output

Total Elements in list: 5 Total Elements in tuple: 5 Total Elements in set: 5 Total Elements in dict: 5 string length: 11 byte length: 5 range[] length: 10

It throws the TypeError if the specified value is bool or int.

Example: len[]

Copy

print[len[True]] print[len[100]]

Output

TypeError: object of type 'bool' has no len[] TypeError: object of type 'int' has no len[]

Syntax:

len[value]

Parameters:

Value: the given value you want the length of.

Return value

It will return an integer value i.e. the length of the given string, or array, or list or collections.

Various types of Return values:

Strings:

It returns the number of characters in a string, which includes punctuation, space, and all type of special characters. However, you should be very careful while using len of a Null variable.

Empty:

Empty is a second return call which has zero characters, but it is always None.

Collections:

The len built-in returns the number of elements in a collection.

TypeError:

Len function depends on the type of the variable passed to it. A Non-Type does not have any built-in support.

Dictionary:

For the dictionary, each pair is counted as one unit. However, values and keys are not independent.

Example 1: How find the length of the given string?

# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]

Output:

The length of the string is : 35

Example 2: How to find the length of the list in python?

# to find the length of the list list1 = ["Tim","Charlie","Tiffany","Robert"] print["The length of the list is", len[list1]]

Output:

The length of the list is 4

Example 3: How to find the length of a tuple in python

# to find the length of the tuple Tup = ['Jan','feb','march'] print["The length of the tuple is", len[Tup]]

Output:

The length of the tuple is 3

Example 4: How to find the length of the dictionary in Python?

# to find the length of the Dictionary Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25} print["The length of the Dictionary is", len[Dict]]

Output:

The length of the Dictionary is 4

Example 5: How to find the length of the array in python

# to find the length of the array arr1 = ['Tim','Charlie','Tiffany','Robert'] print["The length of the Array is", len[arr1]]

Output:

The length of the Array is 4

Summary:

  • len[] is a built-in function in python.You can use the len[] to get the length of the given string, array, list, tuple, dictionary, etc.
  • Value: the given value you want the length of.
  • Return value a return an integer value i.e. the length of the given string, or array, or list, or collections.

You Might Like:

  • Python Strings: Replace, Join, Split, Reverse, Uppercase & Lowercase
  • Python File Handling: How to Create Text File, Read, Write, Open
  • PyQt5 Tutorial with Examples: Design GUI using PyQt in Python
  • 10 BEST Python IDE & Code Editors for Windows, Linux & Mac
  • 15 BEST Python Courses Online [Free & Paid] in Feb 2022

Video liên quan

Bài mới nhất

Chủ Đề