0 votes
1 view
in Python by (47.8k points)

How do I access the dictionary inside the array?

import numpy as np

x = np.array({'x': 2, 'y': 5})

My initial thought:

x['y']

Index Error: not a valid index

x[0]

Index Error: too many indices for array

1 Answer

0 votes
by (107k points)

To access the dictionary inside the array you can follow the given steps. The first thing you have a 0-dimensional array of object dtype. This is the mistaking that you have made a 0-dimensional array, You can still use it by extracting the dictionary by indexing the array with a tuple of no indices:

x[()]

or by calling the array's item method:

x.item()

Related questions

0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...