How to use this len function to find the Python list length with few practical examples?. The Python len is used to find the length of any object. Here, we used Python len function to find list length.
The syntax of this len function on List to find python list length is:
len(list_name)
Python List length Example
In this Python list length example, we declared an empty list. Next, we used the len function to find the length of an empty List.
# Python List Length Example # Python Length of an Empty List emptyList = [] print("Length of a List = ", len(emptyList))
List Length of an Integer Example
We are finding the length of an integer list using list len function..
# Python List Length Example # Python Length of an Integer List integerList = [12, 22, 33, 44, 55, 66, 77] print("\n Original List = ", integerList) print("Length of an Integer List = ", len(integerList))
String List length Example
When you use this len function on String List, it returns the total number of words in a string. Or, say, it finds the length of the string items in a list.
This Python example shows the total number of string items or the total number of words in the string List.
# Python List Length Example # Python Length of a String List stringList = ['Krishna', 'John', 'Yung', 'Ram', 'Steve'] print("\n Original List = ", stringList) print("Length of a String List = ", len(stringList))
Python Mixed List Length Example
Apart from regular lists, you can also find the length of the mixed list.
# Python List Length Example # Python Length of a String List mixedList = ['Krishna', 20, 'John', 40.5, 'Yung', 11.98, 'Ram', 22] print("\n Original List = ", mixedList) print("Length of a Mixed List = ", len(mixedList))
Length of Mixed list Example 2
We declared a Tuple inside a list. Next, we are going to find List length (this includes tuple). Remember, len counts the complete Tuple as a single element.
# Python List Length Example # Python Length of a String List mixedList = ['Krishna', 20, 'John', (40, 50, 65), 'Yung', 11.98, 'Ram'] print("\n Original List = ", mixedList) print("Length of a Mixed List = ", len(mixedList))
Python Nested List Length Example
Let us see how to find the length of a nested list. For this, we declared a list inside a list with the combination of a few other items. If you want the complete list length, then it considers the Nested list as one element. However, you can get the Nested list length using the index value. For example, the below code is finding the length of a nested list [20, 40, 50, 65, 22].
# Python List Length Example # Python Length of a String List nestedList = ['Krishna', 20, 'John', [20, 40, 50, 65, 22], 'Yung', 11.98] print("\n Original List = ", nestedList) print("Length of a Nested List = ", len(nestedList[3]))
Dynamic List Length Example
This Program allows the user to enter the total number of list items. Next, using For Loop, it iterates each list position and allows you to enter the individual list items. Within the loop, we used append function to add items to List. Once we got the list items, we are finding the list length using this len function.
# Python List Length Example intList = [] Number = int(input("Please enter the Total Number of Items in a List : ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) intList.append(value) print("\n Original List = ", intList) print("Length of a Dynamic List = ", len(intList))