;

Python Program to Find the Length of List


Tutorialsrack 02/05/2020 Python

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.

Program 1: Python Program to Find the Length of an Empty List

Python Program to Find the Length of an Empty List
# Python Program to Find the Length of an Empty List

emptyList = []
print("Length of a List = ", len(emptyList))
Output

Length of a List =  0

Program 2: Python Program to Find the Length of an Integer List

Python Program to Find the Length of an Integer List
# 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))
Output

Original List =  [12, 22, 33, 44, 55, 66, 77]

Length of an Integer List =  7

Program 3: Python Program to Find the Length of a String List

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))
Output

Original List =  ['Harry', 'Barry', 'Tom', 'Bill', 'Steve', 'Jason']

Length of a String List =  6

Program 4: Python Program to Find the Length of a Mixed List

Python Program to Find the Length of a Mixed List
# 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))
Output

Original List =  ['Harry', 'Barry', 'Tom', 85, 'John', 52.5, 'Yung', 18.98, 'Steve', 32]

Length of a Mixed List =  10

Program 5: Python Program to Find the Length of a Nested List

Python Program to Find the Length of a Nested List
# 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]))
Output

Original List =  ['Harry', 'Barry', 'Tom', 'John', [10, 52, 50, 65, 22], 'Yung']

Length of a Nested List =  5

Program 6: Python Program to Find the Length of a List Dynamically

Python Program to Find the Length of a List Dynamically
# 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))
Output

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


Related Posts



Comments

Recent Posts
Tags