In this python program, we will learn how to find the length of the list. There are two ways to find the length of the list. The first method is by using python built-in function len()
and the second method is by using loops.
Here is the source code of the program to find the length of the list in python.
# Python Program to Find the Length of an Empty List
emptyList = []
print("Length of a List = ", len(emptyList))
Length of a List = 0
# Python Program to Find the Length of an Integer List
integerList = [12, 22, 33, 44, 55, 66, 77]
print("\nOriginal List = ", integerList)
print("Length of an Integer List = ", len(integerList))
Original List = [12, 22, 33, 44, 55, 66, 77]
Length of an Integer List = 7
Python Program to Find the Length of a String List
# Python Program to Find the Length of a String List
stringList = ['Harry', 'Barry', 'Tom', 'Bill', 'Steve','Jason']
print("\nOriginal List = ", stringList)
print("Length of a String List = ", len(stringList))
Original List = ['Harry', 'Barry', 'Tom', 'Bill', 'Steve', 'Jason']
Length of a String List = 6
# Python Program to Find the Length of a Mixed List
mixedList = ['Harry', 'Barry', 'Tom', 85, 'John', 52.5, 'Yung', 18.98, 'Steve', 32]
print("\n Original List = ", mixedList)
print("Length of a Mixed List = ", len(mixedList))
Original List = ['Harry', 'Barry', 'Tom', 85, 'John', 52.5, 'Yung', 18.98, 'Steve', 32]
Length of a Mixed List = 10
# Python Program to Find the Length of a Nested List
nestedList = ['Harry', 'Barry', 'Tom', 'John', [10, 52, 50, 65, 22], 'Yung']
print("\nOriginal List = ", nestedList)
print("Length of a Nested List = ", len(nestedList[4]))
Original List = ['Harry', 'Barry', 'Tom', 'John', [10, 52, 50, 65, 22], 'Yung']
Length of a Nested List = 5
# Python Program to Find the Length of a List Dynamically
intList = []
# Take the Input From the List
Number = int(input("Enter the Total Number of Items in a List: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
intList.append(value)
# Print the List Entered By the User
print("\nOriginal List = ", intList)
print("Length of a Dynamic List = ", len(intList))
Enter the Total Number of Items in a List: 6
Enter the Value of 1 Element: 1512
Enter the Value of 2 Element: 123
Enter the Value of 3 Element: 5421
Enter the Value of 4 Element: 5421
Enter the Value of 5 Element: 413
Enter the Value of 6 Element: 156
Original List = [1512, 123, 5421, 5421, 413, 156]
Length of a Dynamic List = 6
Comments