×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Preferred way to retrieve the length of an array in Python

By Sapna Deraje Radhakrishna Last updated : December 21, 2024

Retrieving the length of an array

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__().

Retrieve the length of an array using __len__()

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

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

Output

3
3

Retrieve the length of an array using len()

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.