Len list Python 3

Get the number of items of a list in Python

Posted: 2021-09-11 / Tags: Python, List
Tweet

In Python, the built-in function len[] is used to get the length [the number of items] of a list.

  • Built-in Functions - len[] Python 3.9.7 documentation

This article describes the following contents.

  • Get the number of items in a list with len[]
  • For two-dimensional lists [lists of lists]
  • For multidimensional lists

See the following article for the usage of len[] for objects of other types.

  • How to use len[] in Python

You can get the total number of elements with len[]. If you want to get the number of occurrences of an element, use the count[] method or Counter of the standard library collections.

  • Count elements from a list with collections.Counter in Python
Sponsored Link

Get the number of items in a list with len[]

By passing a list as an argument of the built-in function len[], its number of items is returned as an integer value.

l = [0, 1, 2, 3] print[len[l]] # 4
source: list_len.py

For two-dimensional lists [lists of lists]

If a two-dimensional list [a list of lists] is passed directly to len[], the number of lists stored as elements is returned.

l_2d = [[0, 1, 2], [3, 4, 5]] print[len[l_2d]] # 2
source: list_len.py

The number of items in each list [the number of items in each row] can be obtained using the list comprehensions.

  • List comprehensions in Python
print[[len[v] for v in l_2d]] # [3, 3]
source: list_len.py

The total number of items can be calculated with sum[].

A generator version of the list comprehensions [= generator expressions] is used here. Generator expressions are enclosed in [], not [], but when they are used within [], as in this example, it is not necessary to write [] twice, and [] of generator expressions can be omitted.

print[sum[len[v] for v in l_2d]] # 6
source: list_len.py

If NumPy is installed in your environment, you can convert it to a NumPy array ndarray.

You can get the total number of items with the attribute size, and the shape [number of rows and columns] with the attribute shape.

  • Convert numpy.ndarray and list to each other
  • NumPy: Get the number of dimensions, shape, and size of ndarray
import numpy as np l_2d = [[0, 1, 2], [3, 4, 5]] arr_2d = np.array[l_2d] print[arr_2d] # [[0 1 2] # [3 4 5]] print[arr_2d.size] # 6 print[arr_2d.shape] # [2, 3]
source: list_len.py
Sponsored Link

For multidimensional lists

Take as an example a list whose elements are lists of varying sizes.

If this is passed to len[], the number of objects stored as elements is returned.

l_multi = [[0, 1, 2, [10, 20, 30]], [3, 4, 5], 100] print[len[l_multi]] # 3
source: list_len.py

Also, if you pass it to numpy.array[], a NumPy array ndarray whose elements are list type objects with varying numbers of elements is generated. size and shape are also calculated for objects stored as elements.

arr_multi = np.array[l_multi] print[arr_multi] # [list[[0, 1, 2, [10, 20, 30]]] list[[3, 4, 5]] 100] print[arr_multi.size] # 3 print[arr_multi.shape] # [3,]
source: list_len.py

To get the total number of elements in a list that stores lists of varying sizes, you can define a function that recursively calculates the number of elements in the list.

def my_len[l]: count = 0 if isinstance[l, list]: for v in l: count += my_len[v] return count else: return 1 l_multi = [[0, 1, 2, [10, 20, 30]], [3, 4, 5], 100] print[my_len[l_multi]] # 10 l_2d = [[0, 1, 2], [3, 4, 5]] print[my_len[l_2d]] # 6 l = [0, 1, 2, 3] print[my_len[l]] # 4
source: list_len.py
Sponsored Link
Share
Tweet

Related Categories

  • Python
  • List

Related Articles

  • List comprehensions in Python
  • Filter [extract/remove] items of a list with filter[] in Python
  • Convert pandas.DataFrame, Series and list to each other
  • Unpack and pass list, tuple, dict to function arguments in Python
  • Unpack a tuple and list in Python
  • Reverse a list, string, tuple in Python [reverse, reversed]
  • Convert 1D array to 2D array in Python [numpy.ndarray, list]
  • in operator in Python [for list, string, dictionary, etc.]
  • Remove/extract duplicate elements from list in Python
  • Convert lists and tuples to each other in Python
  • Cartesian product of lists in Python [itertools.product]
  • enumerate[] in Python: Get the element and index from a list
  • zip[] in Python: Get elements from multiple lists
  • Swap values ​​in a list or values of variables in Python
  • Initialize a list with given size and values in Python

Video liên quan

Chủ Đề