['a', 'b'][True] results 'b' But how?
Hi, Can any one please tell me how is the following code is working? ['a','b'] is a list of string, and [True] is list of boolean value. How is it making effect? code Python24 ['a','b] [True] 'b' ['a','b'] [False] 'a' ['a','b']['some_string' == r'some_string'] 'b' ['a','b']['some_string' == r'somestring'] 'a' code Thanks in advance, regards, kath. -- http://mail.python.org/mailman/listinfo/python-list
Re: ['a', 'b'][True] results 'b' But how?
In this case, [True] and [False] are not lists, rather you're accessing the items of the list with the index True or False, as per the following example: a_list = ['a', 'b'] a_list[True] 'b' a_list[False] 'a' This happens because the __getitem__ method takes its argument (which in this case is True or False) and casts it into an integer: int(True) 1 int(False) 0 So thus it follows logically that since: a_list[1] 'b' a_list[0] 'a' a_list[True] and a_list[False] must be its first and zeroth indexed members, respectively. On 7/4/07, kath [EMAIL PROTECTED] wrote: Hi, Can any one please tell me how is the following code is working? ['a','b'] is a list of string, and [True] is list of boolean value. How is it making effect? code Python24 ['a','b] [True] 'b' ['a','b'] [False] 'a' ['a','b']['some_string' == r'some_string'] 'b' ['a','b']['some_string' == r'somestring'] 'a' code Thanks in advance, regards, kath. -- http://mail.python.org/mailman/listinfo/python-list -- Kelvie -- http://mail.python.org/mailman/listinfo/python-list
Re: ['a', 'b'][True] results 'b' But how?
kath wrote: Hi, Can any one please tell me how is the following code is working? ['a','b'] is a list of string Yes. and [True] is list of boolean value. No. It's the subscription operator applied to the list of strings. a = ['a', 'b'] a[True] may be clearer. How is it making effect? int(True) 1 int(False) 0 isinstance(True, int) True bool.__bases__ (type 'int',) code Python24 ['a','b] [True] 'b' ['a','b'] [False] 'a' ['a','b']['some_string' == r'some_string'] 'b' ['a','b']['some_string' == r'somestring'] 'a' code -- rbh -- http://mail.python.org/mailman/listinfo/python-list
Re: ['a', 'b'][True] results 'b' But how?
True stands for 1 and False stands for 0 so list[True] is equivalent to list[1] and list[False] is equivalent to list[0] On 7/5/07, kath [EMAIL PROTECTED] wrote: Hi, Can any one please tell me how is the following code is working? ['a','b'] is a list of string, and [True] is list of boolean value. How is it making effect? code Python24 ['a','b] [True] 'b' ['a','b'] [False] 'a' ['a','b']['some_string' == r'some_string'] 'b' ['a','b']['some_string' == r'somestring'] 'a' code Thanks in advance, regards, kath. -- http://mail.python.org/mailman/listinfo/python-list -- Regards-- Rishi Pathak National PARAM Supercomputing Facility Center for Development of Advanced Computing(C-DAC) Pune University Campus,Ganesh Khind Road Pune-Maharastra -- http://mail.python.org/mailman/listinfo/python-list