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 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 items from one list in another
You can use Except:
List<car> list1 = GetTheList();
List<car> list2 = GetSomeOtherList();
List<car> result = list2.Except(list1).ToList();
You probably don't even need those temporary variables:
List<car> result = GetSomeOtherList().Except(GetTheList()).ToList();
Note that Except
does not modify either list - it creates a new list with the result.
How to Remove elements from one List based another list's element and condition?
You can remove the elements like this:
list1.RemoveAll(item => list2.Any(item2 => item.Key == item2.Key))
How to remove all items in a list that are in another list in Python 3?
Use the filter
function to filter out any items present in both lists like this:
def array_diff(a, b):
return list(filter(lambda item: item not in ls2, ls1))
ls1 = [1, 2, 3, 4, 5]
ls2 = [2, 5]
print(array_diff(ls1, ls2))
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']
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)
.
Related Topics
How to Deserialize Xml to Object
Get a Screenshot of a Specific Application
Casting Interfaces for Deserialization in JSON.Net
How to Open Visual Studio Code with Admin Privileges to Make Effect of the Installed Extensions
How to Change a Private Readonly Field in C# Using Reflection
How to Detect If I'm Running in Mono-Service
Run Async Method Regularly with Specified Interval
How to Set Cursor Position in Gtk -Linux, Monodevelop
How to Create a Simple Proxy in C#
Temporarily Clear the Terminal in Linux
How to Get Object Size in Memory
Identifying Last Loop When Using for Each
How to Programmatically Generate Keypress Events in C#
Why Is Multiple Inheritance Not Allowed in Java or C#
Xml Serialization and Namespace Prefixes
How to Get the Name of the Current Method from Code
How to Send Sms Through C# Code Using System.Io.Ports Using Gsm Modem