By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Simple question on indexing

P: n/a

Hi,
I would like to obtain the position index in a tuple when an IF
statement is true. Something like
>>>a=['aaa','bbb','ccc']
[ ??? for name in a if name == 'bbb']
1
but I'm not able to find the name of the function ??? in the python documentation, any help?
Thanks

Dec 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Tartifola wrote:
Hi,
I would like to obtain the position index in a tuple when an IF
statement is true. Something like
>>a=['aaa','bbb','ccc']
[ ??? for name in a if name == 'bbb']
1

but I'm not able to find the name of the function ??? in the python documentation, any help?
Thanks
Ummm, that's a list not a tuple: I'll assume you meant sequence.

This will generate a list of indexes which match the criteria:
>>a = [ 1, 2, 3, 2, 5, 7]
[elno for elno,el in enumerate(a) if el == 2]
[1, 3]

hth
Jon.

Dec 1 '06 #2

P: n/a
On Friday 01 December 2006 17:21, Tartifola wrote:
I would like to obtain the position index in a tuple when an IF
statement is true. Something like
>>a=['aaa','bbb','ccc']
[ ??? for name in a if name == 'bbb']
1
What about:

[ x for x,y in enumerate(a) if y == 'bbb' ]

Or if there is only one element 'bbb':

a.index('bbb')

Kindly
Christoph
Dec 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.