What is the most efficient way to extract to a list the indices corresponding to the n highest values of another list, while preserving the list we're extracting the indices from?
For example, let's say we have the following list of indices:
foo = [107,6,34,12,82]
If we requested the indices of the 2 highest values of the list foo, it should return us the following list:
bar = [0,4]
Here's what I'm running right now, it's really inefficient and not elegant at all and I don't really know how to improve it:
foo = [107, 6, 34, 12, 82]
tmp = list(foo)
bar = []
no_of_indices_wanted = int(input())
for n in range(no_of_indices_wanted):
bar.append(foo.index(max(foo)))
foo.pop(foo.index(max(foo)))
foo = tmp