How to get the address for an element in Python array?
Last Updated :
23 Aug, 2021
In this article we are going to discuss about getting the address of an particular element in the Python array. In python we can create the array using numpy. Numpy stands for numeric python used to create and process arrays. We have to import numpy module
import numpy as np
Syntax to create array:
np.ndarray([element1,element2,....,element n])
Method 1: Using data
we can get the address by using data through array index.
Syntax:
array[index].data
It will return the memory of that element present at the given index. Given below are implementations for the same.
Example: Python code to create an array of 10 elements and access the memory of some elements
Python3
# import numpy module
import numpy as np
# create an array of 10 elements
a = np.array([1, 2, 3, 4, 5, 6, 7, 34, 56, 78])
# get index 4 element address
print("The element present at 4 th index - element",
a[4], ":", a[4].data)
# get index 5 element address
print("The element present at 5 th index - element",
a[5], ":", a[5].data)
# get index 1 element address
print("The element present at 1 st index - element",
a[1], ":", a[1].data)
# get index 0 element address
print("The element present at 0 th index - element",
a[0], ":", a[0].data)
Output:
The element present at 4 th  index - element 5 : <memory at 0x7f9e01221680>
The element present at 5 th index - element 6 : <memory at 0x7f9e01221680>
The element present at 1 st index - element 2 : <memory at 0x7f9e01221680>
The element present at 0 th  index - element 1 : <memory at 0x7f9e01221680>
Method 2 : Using __array_interface__
We can get all the memory details by using this method so obviously memory address will also be returned.
Syntax:
arr[index].__array_interface__
Example: Python code to get the address details of array elements
Python3
# import numpy module
import numpy as np
# create an array of 10 elements
a = np.array([1, 2, 3, 4, 5, 6, 7, 34, 56, 78])
# get index 4 element address
print("The element present at 4 th index - element",
a[4], ":", a[4].__array_interface__)
# get index 5 element address
print("The element present at 5 th index - element",
a[5], ":", a[5].__array_interface__)
# get index 1 element address
print("The element present at 1 st index - element",
a[1], ":", a[1].__array_interface__)
# get index 0 element address
print("The element present at 0 th index - element",
a[0], ":", a[0].__array_interface__)
Output:
The element present at 4 th  index - element 5 : {'data': (94734975551568, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (), 'version': 3, '__ref': array(5)}
The element present at 5 th index - element 6 : {'data': (94734975551568, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (), 'version': 3, '__ref': array(6)}
The element present at 1 st index - element 2 : {'data': (94734975551568, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (), 'version': 3, '__ref': array(2)}
The element present at 0 th  index - element 1 : {'data': (94734975551568, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (), 'version': 3, '__ref': array(1)}
Similar Reads
NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4
2 min read
How to get value from address in Python ? In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
4 min read
How to get the memory address of an object in Python In Python, everything is an object, from variables to lists and dictionaries everything is treated as objects. In this article, we are going to get the memory address of an object in Python. Method 1: Using id() We can get an address using the id() function. id() function gives the address of the pa
2 min read
How to Retrieve an Entire Row or Column of an Array in Python? Retrieving an entire row or column from an array in Python is a common operation, especially when working with matrices or tabular data. This can be done efficiently using different methods, especially with the help of NumPy. Letâs explore various ways to retrieve a full row or column from a 2D arra
3 min read
How to access array Elements in Ruby In this article, we will learn how to access array elements in Ruby. In Ruby, there are several ways to retrieve the elements from the array. Ruby arrays provide a lot of different methods to access the array element. But the most used way is to use the index of an array. Using Index - ruby # Ruby p
2 min read