def drop dest(routes,location):
for i in range(len(routes)):
if routes[i] == location:
routes.remove(routes[i])
return routes
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
if that is your code and needs to be in a loop and removed only once I would do it like that:
def drop_dest(routes,location):
flag = 1
for i in range(len(routes)):
if routes[i] == location and flag == 1:
routes.remove(routes[i])
flag = 0
return routes´
It's simple, use list.remove
.
>>> routes = [(3,2),(2,4),(5,5),(2,4)]
>>> routes.remove((2,4))
>>> routes
[(3, 2), (5, 5), (2, 4)]