Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.4k answers

501 comments

693 users

Browse Categories

...