If you want the count if the elements appear anywhere, get the union of all the sublists and see how many times each element from l2 appears in it:
l = [['20011', '20048'],
['20011', '20048'],
['20011', '20048'],
['20011', '20048']]
l2 = ['20011', '20048' ,'20011', '20048']
union = set.union(*map(set,l))
print(sum(ele in union for ele in l2)) # -> 4
If you don't want to count unique elements more than once, get the intersection:
l = [['20011', '20048'],
['20011', '20048'],
['20011', '20048'],
['20011', '20048']]
l2 = ['20011', '20048', '20011', '20048']
inter = set.union(*map(set, l)).intersection(l2)
print(len(inter)) # -> 2
If you want to use the elements from the sublists for the count:
l = [['20011', '20048'],
['20011', '20048'],
['20011', '20048'],
['20011', '20048']]
l2 = ['20011', '20048', '20011', '20048']
st = set(l2)
from itertools import chain
print(sum(ele in st for ele in chain.from_iterable(l)))
To count based on the sublist being disjoint or not from n
, you can use set.isdisjoint
so if there is any common elements not st.isdisjoint(sub)
will be True.:
l = [['20011', '20048'],
['20011', '20048'],
['20011', '20048'],
['20011', '20048']]
l2 = ['20011', '20048', '20011', '20048']
st = set(l2)
print(sum(not st.isdisjoint(sub) for sub in l)) # -> 4