Simple question on indexing

J

Jon Clements

Tartifola said:
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.
 
C

Christoph Haas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top