CodeBlessU
learn code with examples

Python Length

Use built-in len() function to find length / number of elements in sequence.

Argument is mandatory.

If input sequence is empty, it returns 0.

Syntax:
len(variable)
Return Value:
Zero or positive number
Use
  • len - How many elements?
  • .count() - How many times this element is present?
  • .index() - What is the index / position of this element?

List length, Size of List

Use built-in len() function to find length of list. It will return number of elements present in the list.

It can be also referred as size of list.

# Example, find length of list
a1 = [1,2,3,4]
print( len(a1) ) # prints 4

a1 = []
print( len(a1) ) # prints 0

Tuple length, Size of Tuple

Use built-in len() function to find length of tuple. It will return number of elements present in the tuple.

It can be also referred as size of tuple.

# Example, find length of tuple
a1 = (1,2,3,4)
print( len(a1) ) # prints 4

a1 = ()
print( len(a1) ) # prints 0

Array length, Size of Array

Use built-in len() function to find length of array. It will return number of elements present in the array.

It can be also referred as size of array.

# Example, find length of an array
import array

a1 = array.array('i', (1, 10, 100) )
print( len(a1) ) # prints 3

a1 = array.array('i' )
print( len(a1) ) # prints 0

In this page...

Copyright © All rights reserved.