['a', 'b'][True] results 'b' But how?

Robert Bauck Hamar roberth+news at ifi.uio.no
Thu Jul 5 03:23:38 EDT 2007


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



More information about the Python-list mailing list