def drop dest(routes,location):
for i in range(len(routes)):
if routes[i] == location:
routes.remove(routes[i])
return routes
I am using a function definition given a list as routes = [(3,2),(2,4),(5,5),(2,4)]
, and say I just want to remove the first occurrence value of (2,4)
. I am a little confused in how to do this because I do remove the value but I also remove the other value given. Where I just want to remove the first given value.
Using a Loop to remove the first occurrence of character in String. Create an empty string to store our result and a flag set to False to determine whether we have encountered the character that we want to remove. Iterate through each character in the original string.
How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.
shift() The shift() method removes the first element from an array and returns that removed element.
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With