-1

I have a list of indices,

ind = [0,1,2,5,6,7]

And another list of my data:

data = [0,1,4,9,16,25,36,49,64]

I'd like to only keep the indices of data that correspond to the values in list:

result = [0,1,4,25,36,49]

Or reworded, delete the indices in data that aren't in the values of list.

I think there's some kind of list comprehension I could do but I can't figure it out! Thanks,

rh1990
  • 880
  • 7
  • 17
  • 32

1 Answers1

3

You don't need to delete anything, just use a list comprehension to index out of data using the indices from ind

>>> result = [data[i] for i in ind]
>>> result
[0, 1, 4, 25, 36, 49]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218