-1

I want to use "if not in" syntax in python but what I want to do is to if the element is found in list I want to get the index.

for example

if 3 not in [2,3,4]:
    print "hello"

If 3 is found in list I want to get the index which in this case is 1.

sameedhayat
  • 61
  • 1
  • 8

1 Answers1

1
>>> a=[2,3,4]
>>> a.index(3)
1

EDIT

>>> a=[2,3,4]
>>> def check(n):
...     try:
...             print a.index(n)
...     except ValueError:
...             print "Element not found in list a"
... 
>>> check(5)
Element not found in list a
>>> check(3)
1
g4ur4v
  • 3,210
  • 5
  • 32
  • 57