To remove the first occurrence of an element, use list.remove:
>>> xs = ['a', 'b', 'c', 'd']
>>> xs.remove('b')
>>> print(xs)
['a', 'c', 'd']
To remove all occurrences of an element, use a list comprehension:
>>> xs = ['a', 'b', 'c', 'd', 'b', 'b', 'b', 'b']
>>> xs = [x for x in xs if x != 'b']
>>> print(xs)
['a', 'c', 'd']
Answer from Johannes Charra on Stack OverflowHow to remove specific element from an array using python - Stack Overflow
How to remove values to a python array?
Python: How to remove element by index
how can I remove rows from a numpy array based on a condition
Videos
To remove the first occurrence of an element, use list.remove:
>>> xs = ['a', 'b', 'c', 'd']
>>> xs.remove('b')
>>> print(xs)
['a', 'c', 'd']
To remove all occurrences of an element, use a list comprehension:
>>> xs = ['a', 'b', 'c', 'd', 'b', 'b', 'b', 'b']
>>> xs = [x for x in xs if x != 'b']
>>> print(xs)
['a', 'c', 'd']
Usually Python will throw an Exception if you tell it to do something it can't so you'll have to do either:
if c in a:
a.remove(c)
or:
try:
a.remove(c)
except ValueError:
pass
An Exception isn't necessarily a bad thing as long as it's one you're expecting and handle properly.
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]
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.