Saturday, April 13, 2024
HomeSoftware DevelopmentPython Array size - GeeksforGeeks

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() operate. The len() operate returns the variety of components 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() Technique

The Python len() operate can be utilized to search out the size of an array in Python. The len() operate returns the size of the record 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() operate 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 will additionally use Python Numpy module to create an array after which use the len() operate 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 Listing?

The primary variations between a Python array and an inventory are as follows:

Python arrays are homogeneous, which means they’ll solely retailer components of the identical information kind

Python lists can retailer components of various information sorts.

Python arrays are usually extra memory-efficient than lists. They’re applied as a contiguous block of reminiscence

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

Python arrays provide fundamental operations resembling indexing, slicing, appending, and component 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 result of their reminiscence effectivity and the power to leverage sure mathematical libraries within the case of a big assortment of homogeneous components 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

Most Popular

Recent Comments