1

I have a dictionary where I'd like to select multiple keys by index that are out of order. Something like this works for keys in order -

list(result)[0:2]

But I would like only index 0 and 2 -

list(result)[0,2]

I've tried a number of varieties of this, but what am I missing? Thank you

paranormaldist
  • 489
  • 5
  • 16
  • 2
    dictionaries don't have indicies. They are *maps* from keys to values. If you need to do this, you should consider an alternative data structure – juanpa.arrivillaga Mar 04 '21 at 18:43
  • Does this answer your question? [Explicitly select items from a list or tuple](https://stackoverflow.com/questions/6632188/explicitly-select-items-from-a-list-or-tuple) or [Access multiple elements of list knowing their index](https://stackoverflow.com/q/18272160/4518341) – wjandrea Mar 04 '21 at 18:45

1 Answers1

3

You can use a list comprehension.

result = list(result)
new_result = [result[i] for i in list_of_indices]
luthervespers
  • 258
  • 1
  • 2
  • 10