Python

Python len Function

One of the inbuilt methods in Python is len(). It finds the length of any item. It will provide the list’s item count. This method works with a broad range of data types. Although not all data structures seem to be suitable for the len() function, the method accepts an item as an input and outputs that item’s length.

Len() method never has to traverse through the dataset unless we utilize some inbuilt attribute values. The attribute of an item that is retained would be the item’s length. Len() function provides the parameter length value, which is altered every time an element is inserted into or deleted from the dataset. Len() method will operate effectively as a result of this. In this section, we will talk about the more len() method.

Example no. 1:

For some inbuilt sequences, we will use len() in this example. A unit having sorted objects is called a sequence. Some of the major inbuilt orders include lists, tuples, and strings.

a = "hello dear!"
len(a)
print (len(a));
working_days = ["Saturaday", "Sunday"]
len(working_days)
print (len(working_days));
America_coordinates = (45.9844, -1.7364)
len(America_coordinates)
print (len(America_coordinates));

 

We declare a variable ‘a’ and assign it a value in the form of the string ‘hello dear!’ and then we apply the len() function to acquire the length of the string. After this, we apply the print() function to show the determined length. Similarly, we declare a second variable named ‘working_days’ and give the names of two days to this variable. The values specified for this variable are in the shape of a list.

We call the len() function to find the length of this variable also. The print() function prints that length. Now we initialize a variable ‘america_coordinates’. Here we will provide two coordinates. The len() function is being applied to acquire the length of the tuple. In the end, the print() function is called to display the length.


We see that all these data types (strings, tuples, and lists) will provide the useable parameters of the function len().

Example no. 2:

Since the method len() counts the list of elements in the attribute that we want to provide, this function always gives an integer in the output. If the parameter is an empty string, this method returns 0.

len("")
print (len(""));

len([])
print (len([]));

len(())
print (len(()));

 

In this example, we define three different methods of defining an empty sequence. Then we find the length of these sequences with the help of the len() method. The first empty sequence is defined by the use of double quotation marks. The second sequence is defined by using square brackets[]. The third one is defined using round brackets. We have been using the print() method to show the length of these sequences.


In the preceding instance, we can calculate the length of any blank string, blank list, or blank tuple. For every condition, the method len() returns 0.

Example no. 3:

We may make a series by employing the range() function. A range item creates values as required rather than keeps all of them. Len() method will be applied to determine the length of the element provided to range().

len(range(1, 50, 5))
print (len(range(1, 50, 5)));

 

Here we apply range() functionality to set the numbers. Then we utilize the len() method to get the length of this range. Along with this, the print() function will be used to display the calculated length.


The numbers in the defined range start at 1 and end at 49, with 5 as the unit of increment. The initial, end, and step parameters can be used to calculate the length of the specified range element.

Example no. 4:

The method len() is likewise applicable to certain other inbuilt orders. Sometimes we have to determine how many distinct elements there are in a list as well as in any other series. To accomplish this, utilize set() method and len() method.

import random

num = [random.randint(1, 30) for _ in range(30)]

num = [13, 18, 29, 10, 27, 24, 16, 9, 24, 7, 26, 10, 17, 20, 28, 14, 3, 14, 12, 15, 1, 2, 4, 5, 6, 8, 11, 13]

unique_numb = set(num)
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 3, 15, 17}

len(unique_numb)
print (len(unique_numb));

 

At the start of the program, we will integrate the random library. Then we initialize a variable ‘num’. To define the elements, we will utilize the randint() method of the random header file.

Within this function, we apply for loop and range() method. We specify the number of elements that we want to add to the set.

In the next line, we declare a new variable named ‘unique_numb’. Here we call the function set(). The len() function is then applied to this set. After getting the length, we will call the print() function to print that length.


Thirty random values between 1 and 30 are created using list comprehension and are stored in the variable ‘num’. As we’re obtaining random data, the result will vary every time the function is executed. The collection of thirty randomly created values in this specific iteration has seventeen different integers.

Example no 5:

We will frequently utilize the dictionary, which is also an inbuilt data type in Python. Every entry in a dictionary is composed of a key-value combination. Len() method returns the number of entries in the dictionary when the specific dictionary is given as a parameter.

len({"Ahan": 30, "Mureed": 22, "Rabail": 15})
print(len({"Ahan": 30, "Mureed": 22, "Rabail": 15}));

 

In this case, we are going to define different elements in the form of a dictionary. First, we find the length of this dictionary by the use of the len() method. Then we employ the print() function to just depict the length as an output.


As demonstrated by the output of this example, only three key-value sets are present within the defined dictionary.

Conclusion

By the time we were done reading this article, we understood when to utilize the len() method and how to do it effectively. We’ve looked at using the len() method to figure out how lengthy the elements of various collections are, including dictionaries and list comprehensions. In this guide, we also examined how to determine the size of inbuilt data types. We also recognize that some data types serve as appropriate parameters for the len() method.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content