How can i return the second element in the list that starts with “b”

后端 未结 4 763
误落风尘
误落风尘 2021-01-25 18:09

I have this function with lists that has strings in it and I have to find the second element in this list that starts with \"b\".

for example:

second_e         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 19:00

    Try this :

    def  second_elemnt_starting_with_b(list_):
        return [i for i in list_ if i.startswith('b')][1]
    
    print(second_elemnt_starting_with_b(["b", "a", "bb"]))
    

    Output :

    'bb'
    

提交回复
热议问题