-1

I am trying to build a graphics generator function that requires the location of data in the list I want to use the built-in filter function of python to get a list of the locations of the relevant data instead of their values. Is it possible to do that?

I understand there are other methods to achieve this but is it at all possible to do this using the Filter() function?

BigZee
  • 456
  • 5
  • 22

1 Answers1

2

You can use enumerate to turn your list into a list of two-item tuples, with the first item of each tuple being the location (we usually call it the position, or index) of the item, and the second item being the item in the original list at that position.

The following code filters a list of [4, 1, 3, 5, 3] for items with value greater than 3 and returns the positions of such items.

a = [4, 1, 3, 5, 3]
print(list(map(lambda i: i[0], filter(lambda i: i[1] > 3, enumerate(a)))))

This outputs:

[0, 3]
blhsing
  • 91,368
  • 6
  • 71
  • 106