Proposal to Add New Method to List Object: Mutable Sequence Types

Chris Rebert clp2 at rebertia.com
Thu May 14 04:06:59 CEST 2009


On Wed, May 13, 2009 at 1:55 AM, VenkataRamaKrishna Boddu
<bvrkchowdary at yahoo.co.in> wrote:
> Hi All,
>
> This is my first mail to python-list.
>
> I just want to propose the idea of introducing a new method for the List
> Object in similar lines of Dictionary Object has_key method.

has_key has been removed in Python 3.0; just use the `in` operator
instead. The `in` operator also works on lists.

Example use:
z = ["a", "b", "c"]
print("b" in z) #==> True
print("d" in z) #==> False

> See my below code, It always produces the ValueError, if I want to check
> whether an item is present or not, then they is no method in the List Object
> (do a "dir(list)").

You were looking for "__contains__"; it's called by the `in` operator.

Cheers,
Chris
-- 
http://blog.rebertia.com

> So., Is it not good, to have a method in List Object, may be something like
> has_item to check whether a item is present or not?
>
> #============== CODE ==============================
> partsOfMyName = ['Python', 'Perl', 'PHP', 'MySQL'];
> ## strSearchKey = 'Perl';
> strSearchKey = 'Prl';
>
> indexofKey = partsOfMyName.index(strSearchKey);
> print 'Index of search key: %s:  %d'%(strSearchKey, indexofKey);
>
> Thanks
> Venkata Rama Krishna Boddu
> ________________________________
> Explore and discover exciting holidays and getaways with Yahoo! India Travel
> Click here!
> --
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list