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

Here, we will write a Python program where we will find the length of the list. We will see different ways to perform the task.
Submitted by Shivang Yadav, on April 06, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

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

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

Python List Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.