0

I have a list U of lists such that U=[a[1], a[2], a[3]] and where a = [[0,1,2], [0,0,0],[1,1,1]]. I am trying to change U in a way that it will only contain lists without 2. I know that if I want U = [a[2], a[3]] I have to do it like this:

for j in range (0,2):
    U= [f for f in U if f[j]!=2]

But now I want it to return only the index of the lists, I mean at the end U should be [2,3]. Is it possible to do it ?

Thanks for the help !

alko
  • 46,136
  • 12
  • 94
  • 102

3 Answers3

2

Use enumerate:

[i for i, j in enumerate(a) if 2 not in j]

note that list indices in Python start from 0, not from 1

alko
  • 46,136
  • 12
  • 94
  • 102
2

Use enumerate:

>>> a = [[0,1,2],[0,0,0],[1,1,1]]
>>> [i for i, x in enumerate(a, 1) if 2 not in x]
[2, 3]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

you can use index [U.index(l) for l in U if not 2 in l]

svural
  • 961
  • 1
  • 9
  • 17