In this article, we see how to check the length of a list in some of the easy steps and analysis which one is better.

What is Python List?

The list is an array collection in Python which is capable of storing multiple data types in it. It can store an integer, float, string, boolean, or even list inside a list.

int_list = [1, 2, 3, 4, 5]

print(int_list) # output -> [1, 2, 3, 4, 5]

float_list = [1.1, 2.2, 3.3, 4.4, 5.5]

print(float_list) # output -> [1.1, 2.2, 3.3, 4.4, 5.5]

string_list = ['Geekflare', 'Cloudflare', 'Amazon']

print(string_list) # output -> ['Geekflare', 'Cloudflare', 'Amazon']

boolean_list = [True, False]

print(boolean_list) # output -> [True, False]

nested_list = [[1, 2], [1.1, 2.2], ['Geekflare', 'Cloudflare'], [True, False]]

print(nested_list) # [[1, 2], [1.1, 2.2], ['Geekflare', 'Cloudflare'], [True, False]]

different_datatype_list = [1, 1.1, 'Geekflare', True, [1, 1.1, 'Geekflare', True]]

print(different_datatype_list) # output -> [1, 1.1, 'Geekflare', True, [1, 1.1, 'Geekflare', True]]

Python lists can be created using a square bracket or a list constructor function.

square_bracket_list = [1, 1.1, 'Geekflare', True, [1, 1.1, 'Geekflare', True]]

print(square_bracket_list) # output -> [1, 1.1, 'Geekflare', True, [1, 1.1, 'Geekflare', True]]

constructor_list = list((1, 1.1, 'Geekflare', True, [1, 1.1, 'Geekflare', True]))

print(constructor_list) # output -> [1, 1.1, 'Geekflare', True, [1, 1.1, 'Geekflare', True]]

The above square_bracket_list is a list created using square bracket([]), constructor_list is a list created using the list constructor. Both produce the same list output only.

The list can be changeable, allow duplicates in it and be accessible by using an index.

Methods to find List Length

  • len() inbuilt function
  • length_hint method from operator
  • custom function & counter

Method 1: len() inbuilt function

The len() is a python inbuilt function used to find the length of the list and also for other iterables like Set, Tuples, Dictionary.

Example Snippet

languages = ['Python', 'Java', 'C++', 'PHP', 'nodeJS']

languages_length = len(languages)

print('Length of the Language List is: ',languages_length)

Output

Length of the Language List is:  5

I hope you have Python installed, if not, you can use an online Python compiler to practice the code.

Method 2: length_hint method from operator

length_hint is used to return a length of an iterable object (like List, Set, Tuples, Dictionary). It is available inside the python operator module. Not available like other in-built operators.

Example Snippet

import operator

languages = ['Python', 'Java', 'C++', 'PHP', 'nodeJS']

languages_length = operator.length_hint(languages)

print('Length of the Language List using Operator is: ',languages_length)

Output

Length of the Language List using Operator is:  5

Method 3: Custom function & counter

In this method to find the length of the List, we are going to use the traditional method by using for-loop and counter.

For that, we are going to write a function in python. which takes a list or other iterable as argument and return the length of an iterable.

Custom function Snippet

def iterable_count(iterable):
  length = 0
  for item in iterable:
    length+=1
  return length

Example Snippet

def iterable_count(iterable):
  length = 0
  for item in iterable:
    length+=1
  return length

languages = ['Python', 'Java', 'C++', 'PHP', 'nodeJS']

languages_length = iterable_count(languages)

print('Length of the Language List using Custom function is: ',languages_length)

Output

Length of the Language List using Custom function is:  5

Analysing those 3 methods

Performance Analysis for a large list

import timeit # for benchmarking & profiling
import operator

def iterable_count(iterable):
  length = 0
  for item in iterable:
    length+=1
  return length

integer_list = list(range(1, 9999999))

#length check using len()
start_time = timeit.default_timer()
len_length = len(integer_list)
print(timeit.default_timer() - start_time, 'Length of the Integer List using len() is: ',len_length)

#length check using operator.length_hint
start_time = timeit.default_timer()
len_length = operator.length_hint(integer_list)
print(timeit.default_timer() - start_time, 'Length of the Integer List using length_hint is: ',len_length)

start_time = timeit.default_timer()
iterable_count_length = iterable_count(integer_list)
print(timeit.default_timer() - start_time, 'Length of the Integer List using Custom function is: ',iterable_count_length)

Output

3.957189619541168e-06 Length of the Integer List using len() is:  9999998
3.0621886253356934e-06 Length of the Integer List using length_hint is:  9999998
0.4059128537774086 Length of the Integer List using Custom function is:  9999998

As we can see length_hint is faster(3.0621886253356934e-06) when data are in millions. It’s because length hints are used by CPython runtime. Where it is called a python wrapper.

Performance Analysis for a small list

import timeit # for benchmarking & profiling
import operator

def iterable_count(iterable):
  length = 0
  for item in iterable:
    length+=1
  return length

integer_list = list(range(1, 100))

#length check using len()
start_time = timeit.default_timer()
len_length = len(integer_list)
print(timeit.default_timer() - start_time, 'Length of the Integer List using len() is: ',len_length)

#length check using operator.length_hint
start_time = timeit.default_timer()
len_length = operator.length_hint(integer_list)
print(timeit.default_timer() - start_time, 'Length of the Integer List using length_hint is: ',len_length)

start_time = timeit.default_timer()
iterable_count_length = iterable_count(integer_list)
print(timeit.default_timer() - start_time, 'Length of the Integer List using Custom function is: ',iterable_count_length)

Output

7.813796401023865e-07 Length of the Integer List using len() is:  99
1.1278316378593445e-06 Length of the Integer List using length_hint is:  99
3.462657332420349e-06 Length of the Integer List using Custom function is:  99

As we can see len() is faster(7.813796401023865e-07) when data are in thousands or lesser.

In both cases, our custom function with counter takes more time than both methods.

Conclusion

In this article, we understand different ways to check the length of the list and how they fastly check the length of the list.