Home » Python

Preferred way to retrieve the length of an array in Python

Finding length of an array: Here, we are going to learn about the preferred way to retrieve the length of an array in Python.
Submitted by Sapna Deraje Radhakrishna, on February 03, 2020

The __len__() is a method on container types. However, python also provides another option of retrieving the length of an array, using the method len().

The len(abc) usually ends up calling abc.__len__().

Example usage of len() method is as follows,

# array declaration
test_arr = [1,2,3]

# length
print(len(test_arr))
print(test_arr.__len__())

Output

3
3

The len() method works on a tuple, string (which are array of characters) in similar way.

test_tuple = (1,2,3)

# length
print(len(test_tuple))

# string 
test_str = 'include-help'

# length
print(len(test_str))

Output

3
12





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.