for elt in list1:
    bag[elt] += 1
result = []
for elt in list2:
    if elt in bag:
        if bag[elt] == 1:
            del bag[elt]
else:
    bag[elt] -= 1
result.append(elt)
return result
Common elements between two lists not using sets in Python