how-to-subtraction-two-list-in-python
Python has a built-in datatype for an unordered collection of (hashable) things, called a
EDIT: @mdwhatcott points out that you want to check for duplicates.
best-way-to-check-if-a-list-is-empty
set
. If you convert both lists to sets, the comparison will be unordered.set(x) == set(y)
Documentation on set
EDIT: @mdwhatcott points out that you want to check for duplicates.
set
ignores these, so you need a similar data structure that also keeps
track of the number of items in each list. This is called a multiset; the best approximation in the standard library is a collections.Counter
:>>> import collections
>>> compare = lambda x, y: collections.Counter(x) == collections.Counter(y)
>>>
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
>>> compare([1,2,3,3], [1,2,2,3])
False
>>>
best-way-to-check-if-a-list-is-empty
if not a:
print "List is empty"
Using the implicit booleanness of the empty list is quite pythonic.