Stuck and want to know how can we determine size/length of list in Python? We are compiling this guide to answer the question in the simplest possible way.
As we know python programming language contains a lot of functions that will ease your programming journey.
Length of list python
Here we will see the len()
function in python that is used to find the length of a given list.
Syntax of function :
len(Example_list)
Let’s try to understand with an example :
In this example, we will see how to find the length of an empty list with the help of len()
function in python.
Example code :
example_list = [] a = len(example_list) print(example_list) print(’length of the list : ’ , a)
Output:
() length of the list : 0
Explanation of code :
In the above code, the list named empty_list contains nothing that’s why in the output console we are not able to see the elements. And we use the len()
function to find the length of the empty_list.
The len()
returns 0.
[su_label type=”success”]Suggested read[/su_label] Python string to int conversion tutorial
Example 2 :
Now let’s try to find the length of an integer list.
example_list = [3,4,5,7,8,9,1,2,3,4,5,6,7,5,6,7,8] a = len(example_list) print(‘elements of example list : ’ , eample_list) print(‘length of example list : ’ , a)
Output:
elements of example list : 3,4,5,7,8,9,1,2,3,4,5,6,7,5,6,7,8 length of example list : 17
Explanation of code :
In the above code, first, we initialize an integer list called example_list.
And then we pass the example_list in the len()
function that will return the number of elements in the list.
[su_label type=”success”]Suggested read[/su_label] Understanding Python loops
Example 3 :
Let’s try to understand the len()
function with another example. In this example, we will use a list of Strings and we will try to find the length of the list.
example_list = [‘INDIA’,’ USA’,’ RUSSIA’,’BRAZIL’,’ISRAEL ’] a = len(example_list) print(‘the elements of example list : ’ , example_list) print(‘the number of elements in the example list : ’ , a)
Output :
The elements of example list: INDIA USA RUSSIA BRAZIL ISRAEL The number of elements in the example list: 5
Explanation of code
In the first line of code the list variable named example_list initialized by the list of string elements.
Then the variable named contains the return value of len()
function that is an integer type.
The print()
functions will print the elements of the list and the number of elements in the list.
[su_label type=”success”]Suggested read[/su_label] How to compare strings in python
Example 4:
Let’s see one more example in which the list contains mixed elements like integers and strings.
Code :
example_list = [‘INDIA’, ‘USA’,100,62,’0,’RUSSIA’] a = len(example_list) print(‘the elements of example list : ’ , example_list) print(‘the number of elements in the example list : ’ , a)
Output :
The elements of example list: INDIA USA 100 62 0 RUSSIA BRAZIL ISRAEL The number of elements in the example list: 6
[su_label type=”success”]Suggested read[/su_label] Sorting a list in python
Example 5:
Let’s assume a list in the list means a nested list. In this example, we will learn how to find the length of the nested list in python.
Code :
example_list = [‘INDIA’,’ USA’,’ RUSSIA’,[6,7,3,9,5,0,7,3,9,1],’BRAZIL’,’ISRAEL ’] a = len(example_list[3]) print(‘the elements of example list : ’ , example_list) print(‘the number of elements in the nested list : ’ , a)
Output:
The elements of example list: ‘INDIA’,’ USA’,RUSSIA’,[6,7,3,9,5,0,7,3,9,1],’BRAZIL’,’ISRAEL The number of elements in the nested list of the example list: 10
Explanation of code :
In the above code, the first line contains a variable named example_list that has a nested list of integer numbers at the index of 3.
So if you are trying to find out the number of elements in the nested list then you have to pass the index number of the list from where the nested list has started.
As we saw in the above code the nested list started from index number 3 of main list(named example_list). then, we pass the starting index of the nested list. Like this.
len(example_list[3])
Then this function will return the number of elements of the nested list which is starting from index 3.that is 10.
Conclusion :
We have discussed a lot of example for finding the number of elements in the list.
And we have seen that how you can find the number of elements in the list whether the list is an integer list, string list, or a nested list.