Search code examples
pythonlistsimilarity

Beginner in Python: Trying to check for identical items in multiple lists


I'm a beginner trying to create a small Python project to help me find out which ingredient I might be allergic to from a group of four products. Ideally, I would like to input the ingredients of each product into a list format, and then return identical ingredients which appear in two or more of the products.

I'd like to input something like:

product_1 = ["polybutene", "cranberry seed oil", "beeswax"]
product_2 = ["vegetable oil", "beeswax", "shea butter"]

and get a result like:

list1 = ["beeswax"]

I've tried to use compare_intersect but haven't made any progress. Thank you very much in advance!


Solution

  • If you want a list out you can do

    list(set(product_1).intersection(set(product_2)))
    

    Factored out to make each operation clear this looks like

    set_1 = set(product_1)
    set_2 = set(product_2)
    
    intersection = set_1.intersection(set_2)
    
    result = list(intersection)
    

    If you want to preserve duplicates you can implement this efficiently like so.

    from collections import Counter
    def list_intersection(l1, l2):
      c2 = Counter(l2)
      result = []
      for element in l1:
        if c2[element] > 0:  # There are still more of those elements in the other list
          # The element is shared between the lists
          result.append(element)
          c2[element] -= 1
      return result
    

    This will preserve the order of the first list, while only keeping the number of elements of the same type shared between the lists. Here's an example

    list_intersection(['beeswax', 'test', 'other', 'beeswax', 'beeswax'],
                      ['frank', 'green', 'test', 'beeswax', 'beeswax'])
    
    
    prints: ['beeswax', 'test', 'beeswax']