I have two large lists, L1 and L2. L2 is a subset of L1. L1 and L2 both can contain duplicate terms, but I can fairly easily detect/remove/save those if I need to.
I want to write a function that removes all elements from L1 that are also in L2. HOWEVER, if an element in L1 repeats itself (is a duplicate in L1) and is also present in L2, I want to retain one copy of it in the resulting list.
l1 = [1, 2, 2, 3, 4]
l2 = [2, 4]
l3 = question_function(l1, l2)
[1, 2, 3]
[1, 3, 2]
First of all, do not alter
l1 as you iterate over it: that will throw off your iteration indexing and give undesirable results.
Looking at the logic another way,
l3 is composed of
l1elements that do not appear in
l1elements that do appear in
l2, but are in
l1more than once
You can attack this one of two ways: (1) iterate over
l1 and check these conditions for each element; (2) iterate over
l2, identifying elements to remove; then build
l1, removing elements and reducing remaining duplicates as needed.
You can use the
count method to determine whether an item appears more than once, as in
if l1.count(item) > 1: l3.append(item)
Detailed design and coding are left as an exercise for the student. :-)