-1

I have a dictionary and I want to read the values of the different keys. For example, I want to read the values of [0, 3] from the following dictionary:

dic = {0: np.array([2]), 1: np.array([3]), 
       2: np.array([6]), 3: np.array([7]), 4: np.array([8])}

The desired output is also a array which is:

out=[np.array([2]), np.array([7])]
Selcuk
  • 57,004
  • 12
  • 102
  • 110
White cow
  • 85
  • 6
  • Are you aware that `np.array([2])` evaluates to the value of that function, i.e. a numpy array containing the number 2? Printing that would show as `array([2])` and if you store it in a `dict`, that will refer to the same array, not a new `np.array([2])` – Grismar Apr 06 '22 at 00:13
  • Note that `out` as you describe it is not an array, but a Python list. If you need it to be a numpy array, you should describe more clearly what you expect exactly. – Grismar Apr 06 '22 at 00:14

1 Answers1

-1

You can use a list comprehension, e.g.:

out = [dic[i] for i in [0, 3]]
Selcuk
  • 57,004
  • 12
  • 102
  • 110