Change language

Python | Ways to find the length of a list

| |

The list, which is an integral part of daytime Python programming, should be learned by all python users, and knowing its usefulness and operations is essential and always a plus. So this article discusses one such search utility, no. items in the list.

Method 1: Naive method

This method simply starts a loop and increments the counter to the last item in the list to find out quantity. This is the most basic strategy that can be used in the absence of other existing methods.

Code # 1: Demonstration of determining the length of a list using a naive method

# Python code for demo
# list length
# using the naive method

 
# Initializing list

test_list = [ 1 , 4 , 5 , 7 , 8 ]

  
# Print test_list

print ( "The list is:" + str (test_list))

  
# Find the length of the list
# using a loop
# Initialization counter

counter = 0

for i in test_list:

  

  # increment counter

counter = counter + 1

 
# List print length

print ( "Length of list using naive method is:" + str (counter))

Output:

 The list is: [1, 4, 5, 7 , 8] Length of list using naive method is: 5 

Method 2: using len()

The len () method offers the most used and simplest way to find the length of any list. This is the most widely accepted method, accepted by all programmers today.

# Python demonstration program
# len ()

a = [ ]

a.append ( "Hello" )

a.append ( "Geeks" )

a.append ( " For " )

a.append ( " Geeks " )

print ( "The length of list is:" , len  (a))

Output:

 The length of list is: 4 

# Python program to demonstrate how it works
# len ()

n = len ([ 10 , 20 , 30 ])

print ( "The length of list is: " , n)

Exit :

 The length of list is: 3 

Method 3: Using length_hint ()

 

This method is a lesser known method for determining the length of a list. This particular method is defined in the operator class and it may also say no. elements present in the list.

Code # 2: Demonstration of determining the length of a list using len () and length_hint ()

# Python code for demonstration
# length of the list
# using len () and length_hint

from operator import length_hint

 
# Initializing list

test_list = [ 1 , 4 , 5 , 7 , 8 ]

 
# Print test_list

print ( "The list is:" + str (test_list))

 
# Find the length of the list
# using len ()

list_len = len (test_list)

 
# Finding the length of the list
# using length_hint ()

list_len_hint = length_hint (test_list)

 
# Print length list

print ( "Length of list using len () is: " + str (list_len))

print ( "Length of list using length_hint () is: " + str (list_len_hint))

Output:

 The list is: [1, 4, 5, 7, 8] Length of list using len () is: 5 Length of list using length_hint () is: 5 

Analysis performance — Naive versus len () versus length_hint()

When choosing among alternatives it is always necessary to have a good reason to choose one from another. This section analyzes the time it takes to complete all of them to suggest the best choice to use.

Code # 3: Performance Analysis

# Python code for demo
# list length
# Analysis performance

from operator import length_hint

import time

 
# Initializing list

test_list = [ 1 , 4 , 5 , 7 , 8 ]

 
# Print test_list

print ( "The list is:" + str (test_list))

 
# Find the length of the list
# using a loop
# Initialization counter

start_time_naive = time.time ()

counter = 0

for i in test_list:

 

# increment counter

counter = counter + 1

end_time_naive = str (time.time () - start_time_naive)

 
# Find the length of the list
# using len ()

start_time_len = time.time ()

list_len = len (test_list)

end_time_len = str (time. time () - start_time_len)

 
# Find the length of the list
# using length_hint ()

start_time_hint = time.time ()

list_len_hint = length_hint (test_list)

end_time_hint = str (time .time () - start_time_hint)

  
# Print time for each

print ( "Time taken using naive method is:" + end_time_naive)

print ( "Time taken using len () is:" + end_time_len)

print ( "Time taken using length_hint () is:" + end_time_hint)

Output:

 The list is: [1, 4, 5, 7, 8] Time taken using naive method is: 2.6226043701171875e-06 Time taken using len () is: 1.1920928955078125e-06 Time taken using length_hint () is: 1.430511474609375e-06 

< strong> Conclusion: clearly shows that the time taken to do this is naive""  length_hint () " len () hence len () — the best choice to use.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically