Basic code:
var1 = ['b', 'a', 'c', 'd']
var2 = ['c', 'a']
print(set(var1).difference(set(var2)))
Output:
{'b', 'd'}
Question
Is it possible to sort this output into alphabetical order? If so, how can I?
This is what I have tried:
print(set(var1).difference(set(var2)).sort())
But error shows up:
print(set(var1).difference(set(var2)).sort())
AttributeError: 'set' object has no attribute 'sort'
sorted(set(var1).difference(var2))
to turn the set into a sorted list, but you cannot sort the set itself (sets are, by definition, unordered).set(...).difference
can take an arbitrary iterable as its argument; you don't need to wrapvar2
in a set first.