1
a = [5,6,7,8]

print('for list:')
for i in a:
  print((id(i)))

import numpy as np

z = np.array([9,10,11,12])
print('for numpy:')
for i in z:
  print((id(i)))

Note: When i am trying to see the memory address of the values in a list it takes contiguous memory of 32 bits. But when i am converting a list to a Numpy array and printing the address of the values, alternate values are taking same memory address. I don't understand why this is happening?

output:
 
for list:
94620302609024
94620302609056
94620302609088
94620302609120

for numpy:
139853895184240
139853895279216
139853895184240
139853895279216

As you can see for numpy array the memory address of the alternate values are same. Please explain me why this is happening?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Rajat Saha
  • 13
  • 2
  • 1
    did you check https://stackoverflow.com/questions/40196986/numpy-arrays-changing-id? Here are important points to consider `In contrast to a list, values of an array are stored as bytes in a databuffer` & `In general, id is not useful when working with arrays` and most importantly `id` points to memory location in cpython – Epsi95 Jul 31 '21 at 06:39
  • 1
    Not a `machine-learning` question, kindly do not spam irrelevant tags (removed). – desertnaut Jul 31 '21 at 10:06

1 Answers1

1

First of all, id() does not necessarily return a memory address; that it happens to do so in CPython is an implementation detail. It is also important to realize that an id is only valid for as long as the object exists. If an object has been garbage collected, a new object may be created with the same id.

The built-in Python list type does not store objects, it stores references to objects. This is why the same object may be a member of several lists at the same time. (Or dicts, or just plain variables.)

Numpy arrays do not (usually) store Python objects at all — that would be very inefficient, and that is one of the reasons that we use numpy in the first place! This means that when you loop over an ndarray, an object has to be created for each number as it gets pulled out of the array. So the id:s you see are not the id:s of the stored numbers — those are not objects and thus have no id:s! — but the id:s of the temporary objects that are created. And since you are not keeping these objects around, but throwing them away after each iteration of the loop, it is not surprising that a new object sometimes gets the same id as and old object had.

Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15