Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove every occurrence of sub-list from list

I have two lists:

big_list = [2, 1, 2, 3, 1, 2, 4] sub_list = [1, 2] 

I want to remove all sub_list occurrences in big_list.

result should be [2, 3, 4]

For strings you could use this:

'2123124'.replace('12', '') 

But AFAIK this does not work for lists.

This is not a duplicate of Removing a sublist from a list since I want to remove all sub-lists from the big-list. In the other question the result should be [5,6,7,1,2,3,4].

Update: For simplicity I took integers in this example. But list items could be arbitrary objects.

Update2:

if big_list = [1, 2, 1, 2, 1] and sub_list = [1, 2, 1], I want the result to be [2, 1] (like '12121'.replace('121', ''))

Update3:

I don't like copy+pasting source code from StackOverflow into my code. That's why I created second question at software-recommendations: https://softwarerecs.stackexchange.com/questions/51273/library-to-remove-every-occurrence-of-sub-list-from-list-python

Update4: if you know a library to make this one method call, please write it as answer, since this is my preferred solution.

The test should pass this test:

def test_remove_sub_list(self):     self.assertEqual([1, 2, 3], remove_sub_list([1, 2, 3], []))     self.assertEqual([1, 2, 3], remove_sub_list([1, 2, 3], [4]))     self.assertEqual([1, 3], remove_sub_list([1, 2, 3], [2]))     self.assertEqual([1, 2], remove_sub_list([1, 1, 2, 2], [1, 2]))     self.assertEquals([2, 1], remove_sub_list([1, 2, 1, 2, 1], [1, 2, 1]))     self.assertEqual([], remove_sub_list([1, 2, 1, 2, 1, 2], [1, 2])) 
like image 596
guettli Avatar asked Jul 25 '18 12:07

guettli


People also ask

How do I remove all occurrences from a value list?

Use the remove() Function to Remove All the Instances of an Element From a List in Python. The remove() function only removes the first occurrence of the element. If you want to remove all the occurrence of an element using the remove() function, you can use a loop either for loop or while loop.

How do I remove a SUBS list from a list in Python?

Our first approach is to use set comprehension with sorted tuple. In every iteration in the list, we convert the current sublist to a sorted tuple, and return a set of all these tuples, which in turn eliminates all repeated occurrences of the sublists and thus, remove all repeated rearranged sublists.


1 Answers

You'd have to implement it yourself. Here is the basic idea:

def remove_sublist(lst, sub):     i = 0     out = []     while i < len(lst):         if lst[i:i+len(sub)] == sub:             i += len(sub)         else:             out.append(lst[i])             i += 1     return out 

This steps along every element of the original list and adds it to an output list if it isn't a member of the subset. This version is not very efficient, but it works like the string example you provided, in the sense that it creates a new list not containing your subset. It also works for arbitrary element types as long as they support ==. Removing [1,1,1] from [1,1,1,1] will correctly result in [1], as for a string.

Here is an IDEOne link showing off the result of

>>> remove_sublist([1, 'a', int, 3, float, 'a', int, 5], ['a', int]) [1, 3, <class 'float'>, 5] 
like image 80
Mad Physicist Avatar answered Sep 21 '22 14:09

Mad Physicist