There are some similar articles, but I'll leave them because I find the collections.Counter
method to be easy and convenient.
from collections import Counter
hoge = ['a', 'b', 'c', 'd', 'e']
fuga = ['c', 'd', 'e', 'f', 'g']
Counter(hoge + fuga)
# => Counter({'a': 1, 'b': 1, 'c': 2, 'd': 2, 'e': 2, 'f': 1, 'g': 1})
#Two or more elements
[k for k, v in Counter(hoge + fuga).items() if v > 1]
# => ['c', 'd', 'e']
Recommended Posts