Search code examples
pythonlist-comprehension

List comprehension when there are repeated elements in both lists


I want to find all words that have a particular set of target letters. My code works when none of the letters in the target is repeated, but not if there are repeats.

Here is my code:

target = 'bba'
target_words = [
  'aani', 'aaru', 'abac', 'abas', 
  'abba', 'abby', 'abed', 'abel', 'abet'
]
target_list = list(target)


final_list = []
for word in target_words:
    word_list = list(word)
    if (all(x in word_list for x in target_list)):
        final_list.append(word)
        
print('Final list: ', final_list)

The output is Final list: ['abac', 'abas', 'abba', 'abby', 'abed', 'abel', 'abet']

I would like it to be Final list: ['abba', 'abby']

I cannot find a way to get the result I want. It may be because I am converting the letters of the words into a list, but I don't see how to do it otherwise.


Solution

  • This is a good use case for collections.Counter(). It gives us a clean way to check whether the desired overlap exists between a word in target_words and the target, accounting for duplicate letters (with a suggested improvement by azro):

    from collections import Counter
    
    target_counter = Counter(target)
    [word for word in target_words if Counter(word) & target_counter == target_counter]
    

    This outputs:

    ['abba', 'abby']