If I have lists:
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
c = a * b
should give me:
c = [4, 5]
and
c = a - b
should give me:
c = [1, 2, 3]
Is this available for Python or do I have to write it myself?
Would the same work for tuples? I will likely use lists as I will be adding them, but just wondering.
If the order doesn't matter, you can use
set
for this. It has intersection and difference implemented.Here is the info of time complexities of these operations: https://wiki.python.org/moin/TimeComplexity#set. Notice, that the order of subtrahends changes operation complexity.
If element can occur several times (formally it is called
multiset
), you can useCounter
: