How to use len() in python

In some scenarios where you have to traverse across the lists, tuples or any other data structure, you need to know the length of that data structure. For instance, if you’re asked to find a certain item in a list, you need to iterate over the list for which length of a list should be known. Python len() function allows you to find the length of the data by using a single command. In this tutorial, we will learn about how to use len() in python. If you want to learn more about Python Programming, visit Python Programming Tutorials.

In this tutorial, we will cover how to find a length of a string, list, tuple or a dictionary.

Given a string, find its length

The len() function takes the string, list or tuple as an input and then returns its length.

First consider an example in which you have a string of ‘welcome to entechin’ which is stored in a variable greeting. Now, you want to find out the length of that string. Simply, pass this string to len() function and store the returned value in a variable called length. Then, print this length variable on the output window.

greeting = "Welcome to entechin"
length  = len(greeting)
print(length)
19

Given a List, Find ITS LENGTH

Similarly, pass a list consisting of days and find the length of that list

Working_days = ["Tuesday", "Thursday", "Friday", "Saturday"]
length = len(Working_days)
print(length)
3

Given a TUPLE OR A DICTIONARY, Find ITS LENGTH

In the third example, pass a tuple of numbers and a dictionary consisting of different items and their quantity as an input argument to len() function and print out their length.

#Create a tuple
coordinates = (55, -11)
#display its length
print(len(coordinates))

#create a dictionary
items = {"Table": 10, "Glasses": 12, "Cups": 11}
#display its items
print(len(items))
2
3

GIVEN AN ARRAY, FIND ITS LENGTH Using the LEN()

First of all, initialize an array. There are different methods by which you can create an array. We have used the array module of numpy library and created an array of numbers. Then, pass this array to len() function which returns the length of an array.

import numpy as np
numbers = np.array([44, 67, 49, 23, 103, 56])
length  = len(numbers)
print(length)
6

GIVEN A DATAFRAME, FIND A LENGTH OF A PARTICULAR COLUMN

In case of data analysis, you have a huge amount of data stored in the form of a rows and columns in a dataframe. To find the total instances in the dataframe, you need to know the length of a column. Consider an example in which you have the dataframe containing information about earnings of different sectors of a company in every month. We have used the len() command to find the length of dataframe.

import pandas as pd
earning = {
    "June": [60, 75, 90],
    "July": [78, 55, 87],
    "August": [47, 96, 85],
    "Sept.": [68, 88, 69],
}
earning_df = pd.DataFrame(earning, index=["Automation", "Webservices", "Industry"])
print(earning_df)
print(len(earning_df))
3

Leave a Comment

Your email address will not be published.