203

How can I count the number of elements in an array, because contrary to logic array.count(string) does not count all the elements in the array, it just searches for the number of occurrences of string.

UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
  • 1
    This isn't really a duplicate of counting elements in a list, because an array could be higher dimensional, right? The `len()` of an array is not the number of elements in the array unless the array is 1D. You could argue that a list of lists is also higher dimensional, but there's a clearer precedence for the top level list. For this top level list, the number of elements is just its `len()` because it is fundamentally 1D. – EL_DON Jan 19 '18 at 22:56
  • 1
    Wouldn't np.ndarray.size do what you want? E.g. `a= np.ones((3,5,4,8))` gives an array dimensions 3,5,4,8 so has 3*5*4*8 =480 elements. Doing `a.size` returns 480. See [https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html). I would add this as an answer but the Q has been closed as a duplicate, even though it isn't the same as the other question. – gnoodle May 28 '21 at 16:58

5 Answers5

364

The method len() returns the number of elements in the list.

Syntax:

len(myArray)

Eg:

myArray = [1, 2, 3]
len(myArray)

Output:

3

user2925795
  • 400
  • 10
  • 29
Trent
  • 13,249
  • 4
  • 39
  • 36
  • 5
    This only works for a flat, one dimensional list or array, but `print len([[0, 0], [0, 0]])` comes out as 2, as does `len(array([[0, 0], [0, 0]]))`. – EL_DON Jan 19 '18 at 22:49
  • how about index of array? for example, when I'm looping that array, i get some index and how to know how much index is in? – chiper4 May 09 '20 at 13:06
28

len is a built-in function that calls the given container object's __len__ member function to get the number of elements in the object.

Functions encased with double underscores are usually "special methods" implementing one of the standard interfaces in Python (container, number, etc). Special methods are used via syntactic sugar (object creation, container indexing and slicing, attribute access, built-in functions, etc.).

Using obj.__len__() wouldn't be the correct way of using the special method, but I don't see why the others were modded down so much.

Jeremy Brown
  • 17,880
  • 4
  • 35
  • 28
  • Especially when both of us mentioned that is was bad form. Understanding what length "really" does is important in it's own right. – Gregg Lind Oct 14 '08 at 17:11
21

If you have a multi-dimensional array, len() might not give you the value you are looking for. For instance:

import numpy as np
a = np.arange(10).reshape(2, 5)
print len(a) == 2

This code block will return true, telling you the size of the array is 2. However, there are in fact 10 elements in this 2D array. In the case of multi-dimensional arrays, len() gives you the length of the first dimension of the array i.e.

import numpy as np
len(a) == np.shape(a)[0]

To get the number of elements in a multi-dimensional array of arbitrary shape:

import numpy as np
size = 1
for dim in np.shape(a): size *= dim
user2993689
  • 283
  • 3
  • 11
4

Or,

myArray.__len__()

if you want to be oopy; "len(myArray)" is a lot easier to type! :)

Kevin Little
  • 12,436
  • 5
  • 39
  • 47
2

Before I saw this, I thought to myself, "I need to make a way to do this!"

for tempVar in arrayName: tempVar+=1

And then I thought, "There must be a simpler way to do this." and I was right.

len(arrayName)

Evan Young
  • 27
  • 7