remove elements of one list from another list python
I would use a counter of the elements to be removed.
So, something like
from collections import Counter
data = [1, 2, 2, 3, 3, 3]
to_be_removed = [1, 2, 3, 3] # notice the extra 3
counts = Counter(to_be_removed)
new_data = []
for x in data:
if counts[x]:
counts[x] -= 1
else:
new_data.append(x)
print(new_data)
This solution is linear time / space. If you actually need to modify the list (which is code-smell, IMO), you would require quadratic time
Note, consider if you actually just want a multiset - i.e. you could just be working with counters all along, consider:
>>> Counter(data) - Counter(to_be_removed)
Counter({2: 1, 3: 1})
Remove item from one list if NOT in another list?
Assuming playing
is a list of strings corresponding to player names:
bedwarsPoints = [i for i in bedwarsPoints if i[0] in playing]
remove elements in one list present in another list
Use list comprehension:
>>> list1 = ['paste', 'text', 'text', 'here', 'here', 'here', 'my', 'i', 'i', 'me', 'me']
>>> list2 = ["i","me"]
>>> list3 = [item for item in list1 if item not in list2]
>>> list3
['paste', 'text', 'text', 'here', 'here', 'here', 'my']
NOTE: Lookups in lists are O(n)
, consider making a set from list2
instead - lookups in sets are O(1)
.
Remove elements from list that is in another list while keeping duplicates
you need a little bit more complex loops:
l1 = ['a', 'b', 'b', 'a', 'c', 'c', 'a']
l2 = ['c', 'b']
l3=[]
for item in l1:
if item in l2:
l2.remove(item)
else:
l3.append(item)
>>>l3
['a', 'b', 'a', 'c', 'a']
python remove elements of list from another list WITH MULTIPLE OCCURRENCES of items in both
Here is a non list comprehension version for those new to Python
listA = [1, 1, 3, 5, 5, 5, 7]
listB = [1, 2, 5, 5, 7]
for i in listB:
if i in listA:
listA.remove(i)
print listA
How to move multiple elements from one list to another and delete from first list
Generally speaking, you shouldn't try to modify a list as you're iterating over it, as the memory is shifting while you're trying to access it (the mapping between the elements in list_1
and to_move
may not be easily retained if you remove elements from list_1
as well).
Instead, use a list comprehension:
list_1 = [1, 2, 3, 4]
to_move = [True, False, False, True]
list_2 = [elem for index, elem in enumerate(list_1) if to_move[index]]
list_1 = [elem for index, elem in enumerate(list_1) if not to_move[index]]
print(list_1, list_2) # Prints [2, 3] [1, 4]
python remove elements from one list based on indices from another list
To do that, you can use the .pop()
method.
position = [1, 0 ,0 ]
values = [2, 6, 1]
for idx in position:
values.pop(idx)
Related Topics
Why Do Python Classes Inherit Object
Create a Single Executable from a Python Project
Find the Row Indexes of Several Values in a Numpy Array
Finding and Replacing Elements in a List
Select Rows in Pandas Multiindex Dataframe
Refering to a Directory in a Flask App Doesn't Work Unless the Path Is Absolute
How to Clear the Interpreter Console
How to Convert All Strings in a List of Lists to Integers
Efficient Way to Rotate a List in Python
Equivalent of Shell 'Cd' Command to Change the Working Directory
How to Play Wav File in Python
Store Output of Subprocess.Popen Call in a String
Django Template How to Look Up a Dictionary Value With a Variable