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

后端 未结 4 776
误落风尘
误落风尘 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条回答
  •  猫巷女王i
    2021-01-25 18:46

    lst = ["b", "a", "bb"]
    
    print([i for i in lst if i.startswith("b")][1])
    

    Output:

    "bb"
    

    Or as a function:

    def func(lst):
        return [i for i in lst if i.startswith("b")][1]
    

提交回复
热议问题