Given a list of values remove first occurrence

前端 未结 3 1912
旧巷少年郎
旧巷少年郎 2021-01-19 03:39
def drop dest(routes,location):
    for i in range(len(routes)):
        if routes[i] == location:
              routes.remove(routes[i])
    return routes
         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 04:03

    It's very simple. Just use break statement in the loop. So that the loop stops iterating once it satisfies the if condition for the first time. Using remove() is better but if you want to use loop in your code. This might be the answer.

    def drop dest(routes,location):
    for i in range(len(routes)):
        if routes[i] == location:
              routes.remove(routes[i])
              break
    return routes
    

提交回复
热议问题