'How do I return the positions of multiple items in a list? (Python)

I am using a large data set (approx 3600 x values & 3600 y values), and am trying to return the position of certain x values corresponding to y values that have already been pulled out of the original data.

for n in new_y:
    if new_y in y:
        new_x.append(index(y))
print(new_x)

The error code I get states: :43: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. if new_y in y:

Edit: I should have mentioned that when printing new_x, an empty list is returned ([])



Solution 1:[1]

You should use enumerate:

new_x = []
for i, n in enumerate(new_y):
    if n in y:
        new_x.append(i)
print(new_x)

Or, more succinctly:

new_x = [i for i, n in enumerate(new_y) if n in y]

Solution 2:[2]

Use numpy library that way you can slice the data wihtout iterating through the "dataset".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nonDucor
Solution 2 Juan Carlos Moreno