You don't need to iterate the array. Just:
>>> x = ['ala@ala.com', 'bala@bala.com']
>>> x
['ala@ala.com', 'bala@bala.com']
>>> x.remove('ala@ala.com')
>>> x
['bala@bala.com']
This will remove the first occurence that matches the string.
EDIT: After your edit, you still don't need to iterate over. Just do:
index = initial_list.index(item1)
del initial_list[index]
del other_list[index]
Answer from Bogdan on Stack OverflowYou don't need to iterate the array. Just:
>>> x = ['ala@ala.com', 'bala@bala.com']
>>> x
['ala@ala.com', 'bala@bala.com']
>>> x.remove('ala@ala.com')
>>> x
['bala@bala.com']
This will remove the first occurence that matches the string.
EDIT: After your edit, you still don't need to iterate over. Just do:
index = initial_list.index(item1)
del initial_list[index]
del other_list[index]
Using filter() and lambda would provide a neat and terse method of removing unwanted values:
newEmails = list(filter(lambda x : x != 'something@something.com', emails))
This does not modify emails. It creates the new list newEmails containing only elements for which the anonymous function returned True.
How do I remove an element from the end of a list without returning the value? (Python)
Deleting element from list?
Remove Array Items Meeting Condition?
Python: How to remove element by index
Videos
Hi, so I am writing a class for doing some methods on a list.
I already know how to add an element to the end of a list by using the append() method.
However if I want to remove an element from the end of a list, without returning the value, how would I go about that? I know the pop() method can remove an element from the end of a list but it then returns the value of that element. What kind of method can I include in my class that will remove an element from the end of the list?
Hey Guys,
when you're pop or remove to delete an element from a list, does python then resize the list in memory to account for the deleted item? Are all the items after the deleted items swapped one position back up? I'm a bit confused as to how the internal memory structure works for these dynamic arrays. Thanks guys.