Finding the index of an item in a list

后端 未结 30 3323
你的背包
你的背包 2020-11-21 05:28

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

30条回答
  •  心在旅途
    2020-11-21 05:58

    You have to set a condition to check if the element you're searching is in the list

    if 'your_element' in mylist:
        print mylist.index('your_element')
    else:
        print None
    

提交回复
热议问题