0

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.

1 Answers1

2

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())]
Roy2012
  • 11,755
  • 2
  • 22
  • 35
  • 1
    That will be in arbitrary order, while OP wants to use the keys as the list indices. – interjay Jul 04 '20 at 15:02
  • 1
    @interjay. I didn't read the question that way initially, but you may be right. In any case, edited my answer. – Roy2012 Jul 04 '20 at 15:05