Q:

How to find the length of a list in Python (3 effective ways)?

0

The length of a list is the total number of elements present in the list.

Program to find the length of a list in Python

We will get a list as input from the user and then find the length of the entered list.

This can be done either by using loops and there are some functions in-built in python to perform the task

Example:

Input: 
[3, 6, 9, 8, 1]

Output:
5

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1: Using loops

We will loop through the list and then count the number of elements present in the list and print it.

Algorithm:

  • Get the list from the user
  • Initialize the counter with 0
  • Loop till the list has elements,
    • counter += 1
  • Print counter

Program to find the length of a list in Python

# Python program to find the length of a list

# Getting list from user
myList = []
print("Enter list element , (-1 to exit): ")

while(1):
    val = int(input())
    if(val == -1):
        break
    myList.append(val)

# Finding the length of the list 
count = 0
for i in myList:
    count += 1

# Printing list and length
print("List: ", myList)
print("length of the list is: ", count)

Output:

Enter list element , (-1 to exit):
10
20
30
5
1
2
45
-1
List:  [10, 20, 30, 5, 1, 2, 45]
length of the list is:  7

Method 1: Using Python's in-built len() method

Python provides a built-in function for finding the length of the list entered by the user, it is len() function.

Syntax:

len(list_name)

Returns an integer value which is the length of the list.

Program to find the length of a list

# Python program to find the length of a list

# Getting list from user
myList = []
print("Enter list element , (-1 to exit): ")

while(1):
    val = int(input())
    if(val == -1):
        break
    myList.append(val)

# Finding the length of the list 
listLength = len(myList)

# Printing list and length
print("List : ", myList)
print("length of the list is: ", listLength)

Output:

Enter list element , (-1 to exit):
10
20
30
1
5
15
67
-1
List :  [10, 20, 30, 1, 5, 15, 67]
length of the list is:  7

Method 3: Using length_hint()

Python's operator module provides one more way to find the length of the list. It is the length_hint() method.

length_hint()

This method is used to find the number of elements present in an iterable structure. It is present in the operator module in Python's library.

Syntax:

length_hint(iterable_name)

Program to find the length of the list in Python

# Python program to find the length of a list

import operator 

# Getting list from user
myList = []
print("Enter list element , (-1 to exit): ")

while(1):
    val = int(input())
    if(val == -1):
        break
    myList.append(val)

# Finding the length of the list 
listLength = operator.length_hint(myList)

# Printing list and length
print("List: ", myList)
print("length of the list is: ", listLength)

Output:

Enter list element , (-1 to exit):
10
20
30
1
15
67
-1
List:  [10, 20, 30, 1, 15, 67]
length of the list is:  6

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now