How to check whether a list have specific value exist or not?

Michael Spencer mahs at telcopartners.com
Sat Apr 9 05:37:26 EDT 2005


praba kar wrote:
> Dear All
>      In Php we can find in_array() function
> which function is mainly useful to check
> whether a specific value is exist in the array
> or not.
> 
>    But In python In cannot find any function
> like that.  I want to check a list have specific
> value or not. So If any one know regarding this
> mail me
> 
> with regards
> PRabahar
> 
> 
> ________________________________________________________________________
> Yahoo! India Matrimony: Find your life partner online
> Go to: http://yahoo.shaadi.com/india-matrimony

No need to ask this list, ask the built-in one:

  >>> dir(list)
  ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', 
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', 
'__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', 
'__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

A minute of two experimenting, would then lead you to:

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

Note that all indexing in Python is 0-based

Michael




More information about the Python-list mailing list