Python Array size – GeeksforGeeks


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Like Article

In Python, the size of an array could be obtained utilizing the len() perform. The len() perform returns the variety of parts within the array. On this article, we are going to see how we will discover the size of Python Arrays.

Instance:

arr = [1, 2, 3, 4, 5]
Size = 5

Python Array size

Python makes use of Lists as an alternative of arrays. Each are related and are used to work as an array. Allow us to see how we will discover the size of a Python array.

Python Array Size utilizing len() Methodology

The Python len() perform can be utilized to search out the size of an array in Python. The len() perform returns the size of the listing or array.

Python3

arr = [1, 2, 3, 4, 5]

print(len(arr))

Output:

5

Python Array Size utilizing Array Module

Right here we’re going to use the Python array module for creating the array, after which len() perform is used to acquire the size of the array.

Python3

  

import array as arr

  

a = arr.array('i', [1, 2, 3])

  

len(a)

Output:

3

Python Array Size utilizing Numpy Module

We are able to additionally use Python Numpy module to create an array after which use the len() perform to calculate the size of the array.

Python3

  

import numpy as np

  

a = np.array([1, 2, 3])

  

len(a)

Output:

3

What’s the Distinction Between a Python Array and a Checklist?

The principle variations between a Python array and a listing are as follows:

Python arrays are homogeneous, which means they will solely retailer parts of the identical knowledge sort

Python lists can retailer parts of various knowledge sorts.

Python arrays are typically extra memory-efficient than lists. They’re carried out as a contiguous block of reminiscence

Python lists are carried out as dynamic arrays with further options, resembling resizing and insertions/deletions.

Python arrays provide primary operations resembling indexing, slicing, appending, and aspect project.

Python lists, being extra versatile, assist a wider vary of operations, together with insertions, deletions, sorting, reversing, and extra.

Arrays can present higher efficiency as a consequence of their reminiscence effectivity and the flexibility to leverage sure mathematical libraries within the case of a big assortment of homogeneous parts for numerical computations.

Lists, being extra versatile, could have barely decrease efficiency in such eventualities

Final Up to date :
26 Jun, 2023

Like Article

Save Article

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles