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.
asked Oct 9, 2008 at 14:12
UnkwnTechUnkwnTech
85.1k65 gold badges183 silver badges227 bronze badges
2
The method len[] returns the number of elements in the list.
Syntax:
len[myArray]
Eg:
myArray = [1, 2, 3]
len[myArray]
Output:
3
answered Oct 9, 2008 at 14:14
2
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.
answered Oct 9, 2008 at 19:40
Jeremy BrownJeremy Brown
17.3k3 gold badges33 silver badges27 bronze badges
1
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
answered Aug 11, 2015 at 9:03
user2993689user2993689
2433 silver badges11 bronze badges
3
Or,
myArray.__len__[]
if you want to be oopy; "len[myArray]" is a lot easier to type! :]
answered Oct 9, 2008 at 14:23
Kevin LittleKevin Little
12.1k5 gold badges38 silver badges47 bronze badges
4
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]
answered Jul 17, 2015 at 3:05
Hey, folks! I hope you all are doing well. In this article, we will be unveiling 3 variants of Array length in Python. As we all know, Python does not support or provide us with the array data structure in a direct manner. Instead, Python serves us with 3 different variants of using an Array data structure here. Let us go
first go through the different ways in which we can create a Python array. Further, in the upcoming sections, we would be discussing about use of Python len[] method to fetch the length of array in each of the variants. Python provides us with the array data structure in the below forms: We can create an array using any of the above variants and use different functions to work with and manipulate the data. Syntax: Let us now understand the way to find out the
length of an array in the above variants of Python array. In the below example, we have created a list of heterogeneous elements. Further, we have used len[] method to display the length of the list.Finding the Array Length in Python using the len[] method
Python len[] method
enables us to find the total number of elements in the array/object. That is, it returns the count of the elements in the array/object.Finding the Length of a Python List
Python len[] method
can be used with a list to fetch and display the count of elements occupied by a list.lst = [1,2,3,4,'Python']
print["List elements: ",lst]
print["Length of the list:",len[lst]]
Output:
List elements: [1, 2, 3, 4, 'Python'] Length of the list: 5
Finding the Length of a Python Array
Python Array module
helps us
create array and manipulate the same using various functions of the module. The len[] method can be used to calculate the length of the array.
import array as A arr = A.array['i',[1,2,3,4,5]] print["Array elements: ",arr] print["Length of array:",len[arr]]
Array elements: array['i', [1, 2, 3, 4, 5]] Length of array: 5
Finding the Length of a Python NumPy Array
As we all know, we can create an array using NumPy module
and use it for any mathematical purpose. The len[] method helps us find out the number of data values present in the NumPy array.
import numpy as np arr = np.arange[5] len_arr = len[arr] print["Array elements: ",arr] print["Length of NumPy array:",len_arr]
Output:
Array elements: [0 1 2 3 4] Length of NumPy array: 5
Conclusion
By this we have come to the end of this topic. Feel free to comment below, in case you come across any question. Till then, Happy Learning!
References
- Python len[] with List — JournalDev