Carlos de Almeida
Carlos de Almeida

Reputation: 43

Convert dictionary to array in Python 3

I have the following dictionary:

{
'0':'test1'
'1':'test2'
'2':'test3'
'3':'test4'
}

I want to convert it into an array in which the keys, as you can notice, are the indexes, and the key values are the values. So, basically, I want ['test1', 'test2', 'test3', 'test4'].

There is another question like this, which I read, but it doesn't solve my question, since I want the keys of the dictionary as indices.

Upvotes: 0

Views: 151

Answers (1)

Roy2012
Roy2012

Reputation: 12503

if the dictionary is named d, do list(d.values()).

If you know the keys are the numbers 0-N, and you'd like the resulting array to be in that order, do:

[x[1] for x in sorted(d.items())]

Upvotes: 2

Related Questions