You don't need to iterate the array. Just:
>>> x = ['[email protected]', '[email protected]']
>>> x
['[email protected]', '[email protected]']
>>> x.remove('[email protected]')
>>> x
['[email protected]']
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 = ['[email protected]', '[email protected]']
>>> x
['[email protected]', '[email protected]']
>>> x.remove('[email protected]')
>>> x
['[email protected]']
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 != '[email protected]', 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)
How to delete all the items (in a list) beautifully?
python - How to remove an element from a list by index - Stack Overflow
How do I remove elements from a 1D array?
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?
Use del and specify the index of the element you want to delete:
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del a[-1]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Also supports slices:
>>> del a[2:4]
>>> a
[0, 1, 4, 5, 6, 7, 8, 9]
Here is the section from the tutorial.
You probably want pop:
a = ['a', 'b', 'c', 'd']
a.pop(1)
# now a is ['a', 'c', 'd']
By default, pop without any arguments removes the last item:
a = ['a', 'b', 'c', 'd']
a.pop()
# now a is ['a', 'b', 'c']