howtouselinux

3 Examples to Master Python list len() function

Table of Contents

A Python list is a collection of items in a particular order. We can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in our family.

We can put anything we want into a list, and the items in our list don’t have to be related in any particular way. Because a list usually contains more than one element, it’s a good idea to make the name of our list plural, such as letters, digits, or names.

Python counts the items in a list starting with one, so we shouldn’t run into any off-by-one errors when determining the length of a list. In this article, we will share 3 examples of finding the length of a list in Python.

Use List len function to Find the length of list in Python

Len function can used to find the length of a list in Python. This function takes one argument as an input which is the list. It returns an integer value which is the length of the list.

Len() function is a built-in function in python, so you don’t need to import any module to use this function. You can directly use this function by calling it with the list as an input argument.

Len function counts the number of items in some kind of container object, such as a dictionary or list. we’ve seen it before, demonstrated as follows::

Example1:
>>> len([1,2,3,4])
4

Example2:
>>> cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
>>> len(cars)
4

Use list len function in for loop

A common Python technique is to use range(len(someList)) with a for loop to iterate over the indexes of a list.For example, enter the following into the interactive shell:

>>> supplies = [‘pens’, ‘staplers’, ‘flamethrowers’, ‘binders’]
>>> for i in range(len(supplies)):
print(‘Index ‘ + str(i) + ‘ in supplies is: ‘ + supplies[i])

Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flamethrowers
Index 3 in supplies is: binders

Using range(len(supplies)) in the previously shown for loop is handy because the code in the loop can access the index (as the variable i) and the value at that index (as supplies[i]).

Best of all, range(len(supplies)) will iterate through all the indexes of supplies, no matter how many items it contains.

Use List len function in Python to Check Empty list

In the following example, we will initialize an empty list, and check programmatically if the list is empty or not using len() function.

myList = []
if (len(myList) == 0):
print(‘The list is empty.’)
else:
print(‘The list is not empty.’)

Replace replace range(len()) with enumerate function

Instead of using the range(len(someList)) technique with a for loop to obtain the integer index of the items in the list, we can call the enumerate() function instead.On each iteration of the loop, enumerate() will return two values: the index of the item in the list, and the item in the list itself.

>>> supplies = [‘pens’, ‘staplers’, ‘flamethrowers’, ‘binders’]
>>> for index, item in enumerate(supplies):
print(‘Index ‘ + str(index) + ‘ in supplies is: ‘ + item)

Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flamethrowers
Index 3 in supplies is: binder

 

FAQ about len function in Python

lst = [‘abc’, ‘def’, ‘ghi’]
lst[1] = ‘wxyz’
print(len(lst))

A. 3
B. 9
C. 10
D. 4

Answer: A. Changing a list value is allowed (because lists are mutable). But changing the value at index 1 to a longer string doesn’t change the fact that the list has three values.

how to use len function to get the length of string in python?

To use the len function to get the length of a string in python, you need to pass the string as an input argument to the function. The function will return an integer value which is the length of the string of the string.

str = “Hello World”
len(str)
11

how to use len function to get the length of dict in python

To use the len function to get the length of a dict in python, you need to pass the dict as an input argument to the function. The function will return an integer value which is the length of the dict.

dict = {“a”: 1, “b”: 2, “c”: 3}
len(dict)
3

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

If you meet any issues with Linux, record them with Loom and send the link to us here. We will reply ASAP.

Load WordPress Sites in as fast as 37ms!