There're two lists like
[[A, A], [B, B], [C, C], [D, D]]
and
[[A, A], [B, B]]
How to delete list 2 from 1 with result [[C, C], [D, D]]
and make it without loop, because both lists are very big and loops works slow?
thanks list examples
>>>a = [[9, 9], [8, 8], [7, 7], [6, 6], [4, 4], [5, 5], [12, 12], [15, 15], [2, 2], [3, 3]]
>>>b = [[4, 4], [5, 5]]
form ask to write what I already tried, OK, it's below Attempt one: (doesn't work, moreover has a loop inside)
def rightdotsremowe (ax, bx):
for o in set(bx):
try:
ax.remove(o)
except ValueError:
pass
return ax
Attempt two (looks better but doesn't work too)
newlist00 = [x for x in a if (x not in e)]
If the order is not very important to you,sets
are significantly faster. So you can try this,it will be faster than list.
a=[['A', 'A'], ['B', 'B'], ['C', 'C'], ['D', 'D']]
a={tuple(i) for i in a}
And try to use difference
method:
return new set with elements in a but not in b
Average case O(len(a))
a=[['A', 'A'], ['B', 'B'], ['C', 'C'], ['D', 'D']]
b=[['A', 'A'], ['B', 'B']]
a={tuple(i) for i in a}
b={tuple(i) for i in b}
print a.difference(b)
That is becaus set
uses a hash function to map to a bucket. Since Python implementations automatically resize that hash table, the speed can be constant O(1)
.
Sets
are significantly faster when it comes to determining if an object in a set , but slower than lists
when it comes to iterating over their contents.
Hope this helps.