Python is a versatile language that can be used for a variety of purposes. How to find the number of items in a Python list is a commonly asked question during a Python job interview.
In this blog post, we will show you how to find the length of a list in Python.
Table of Contents
Number of items in a Python list
Input: [1,2,3,4,5]
Output: 5
Explanation: The number of elements in the list are 5
Input: [“apple”, “banana”, “mangoe”]
Output: 3
Explanation: The number of elements in the list are 3
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!Using len() to get the number of items in a Python list
The number of items in a Python list can be obtained by using the len() function. The len() function is a built-in function in python that returns the number of items in a python list. To use the len() function, simply put the name of the list inside the parentheses.
For example, if we have a list called my_list, we can find out how many elements are in the list by using len(my_list).
my_list = [‘a’,’b’,’c’]
Print(“The number of elements in my_list is : ” + str(len(my_list)))
The len() function is extremely useful when we need to loop through all the elements in a python list. We can use a for loop to iterate through all the elements in a python list, and use the len() function to determine when to stop the loop.
my_list = [‘one’,’two’,’three’]
for i in range(0,len(my_list)):
print(my_list(i))
In this example, we are using the len() function to determine the number of elements in the list, and using the range() function to determine when to stop the loop.
The range() function is a built-in function in python that returns a list of numbers. The first number in the list is the starting number, and the second number is the stopping number.
For example, if we have a list called my_list with three elements, the len() function will return 3, and the range() function will return a list of numbers from 0 to 2.
So far we have only seen how to use the len() function with a python list. However, the len() function can be used with other data structures as well.
For example, we can use the len() function to find out the number of characters in a string.
my_string = “Hello World”
print(“The number of characters in my_string is : ” + str(len(my_string)))
Similarly, we can use the len() function to find out the number of items in a dictionary.
my_dict = {‘key1′:’value1′,’key2′:’value2′,’key3′:’value3’}
print(“The number of items in my_dict is : ” + str(len(my_dict)))
As we can see, the len() function is a very versatile and useful function in python. It can be used with many different data structures, and it is often very helpful when we need to loop through all the elements in a python list.
Using for loop to get the number of items in a Python list
A for loop is a type of loop that can be used to iterate through all the elements in a python list. To use a for loop, simply put the name of the list inside the parentheses. For example, if we have a list called my_list, we can iterate through all the elements in the list by using a for loop.
my_list = [‘one’,’two’,’three’]
for element in my_list:
print(element)
As we can see, the for loop will iterate through all the elements in the list, and print each element on a separate line.
In order to get the number of elements in a python list, using for loop is considered as a traditional technique.
- Declare a counter variable and initialize it to zero.
- Using a for loop, traverse through all the data elements and after encountering every element, increment the counter variable by 1.
- Thus, the length of the array will be stored in the counter variable. The variable will represent the number of elements in the list.
Example:
List = [‘Python’,’Java’,’Kotlin’,’Machine Learning’,’Keras’]
size = 0
print(“Length of the input string:”)
for x in List:
—-size+=1
—-print(size)
Output:
Length of the input string:
5