Home Finding the index of an item given a list containing it in Python
Reply: 0

Finding the index of an item given a list containing it in Python

user7053
1#
user7053 Published in September 20, 2018, 12:45 pm

For a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?

share|improve this question
  • 2
    Are you returning: [1] The lowest index in case there are multiple instances of "bar", [2] All the indices of "bar"? – Mulliganaceous May 12 at 20:56
  • 1
    a) Is it guaranteed that item is in the list, or else how we should handle the error case? (return None/ raise ValueError) b) Are list entries guaranteed to be unique, and should we return the first index of a match, or all indexes? – smci May 21 at 6:20

25 Answers 25

active oldest votes
up vote 3386 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.84344 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO