Python - checking if phrase appears on a list I have a big file with a lot of "phrases", like:


  sayndz zfxlkl attjtww cti sokkmty brx fhh suelqbp xmuf znkhaes pggrlp
  
  zia znkhaes znkhaes nti rxr bogebb zdwrin sryookh unrudn zrkz jxhrdo
  
  gctlyz bssqn wbmdc rigc zketu ketichh enkixg bmdwc stnsdf jnz mqovwg
  
  ixgken flawt cpott xth ucwgg xce jcubx wvl qsysa nlg qovcqn zxcz
  
  vojsno nqoqvc hnf gqewlkd uevax vuna fxjkbll vfge qrzf phwuf ligf xgen
  
  vkig elptd njdm gvqiu epfzsvk urbltg dqg sfpku viwihi fje umdkwvi
  
  ejzhzj qrbl sfpku sad nawnow ksnku nzhj mfudick ueaa jnhz kpy pzk


What I need to do is check if any 'phrase' didn't appear twice in a line and count lines where it didn't

def check(phrase):
    phrases = phrase.split()
    for i in range(len(phrases)):
        phrase_tmp = phrases.pop(0)
        if phrase_tmp in phrases:
            return False
        phrases.append(phrase_tmp)
    return True


with open('file_with_phrases.txt', 'r') as file:
    counter = 0
    for line in file:
        if check(line):
            counter += 1
    print(counter)


Can you tell me if I did it all good?
Clarifying terms makes both code and documentation easier to understand.  You use the word "phrase" (which usually refers to an arbitrary group of words) to refer to both a line (one of a set of linebreak-delimited strings) and a word (one of a set of whitespace-delimited strings).  If you're talking about a single word, say "word".  :)
Give your function a descriptive name!  Maybe has_unique_words?
Type annotations and a docstring also make functions easier to understand.


Here's what we've got so far:

def has_unique_words(phrase: str) -&gt; bool:
    """Checks whether any (whitespace-delimited) words in the phrase are repeated.
    Returns True only if all words are unique."""
    words = phrase.split()



Mutating an iterable as you iterate over it is an easy way to confuse yourself, and it's usually better to iterate over the elements of an list rather than the range of its indices (although because you're mutating it that might not work well here).  It might be better to find a solution that doesn't involve mutating the list.  Here are a few possibilities that are all simpler alternatives to popping/searching/re-inserting:


# Count the number of occurrences of each word.
# If each occurs exactly once, all words are unique.
word_counts: Dict[str, int] = defaultdict(int)
for word in words:
    word_counts[word] += 1
return all(count == 1 for count in word_counts.values())


# Iterate through each word and check whether we've already seen it.
seen_words: Set[str] = set()  # this could be a list, but a set is faster to check
for word in words:
    if word in seen_words:
        return False
    seen_words.add(word)
return True


This one-line check is what I would do:

# Build a set (which guarantees uniqueness) out of the list of words.
# If the number of elements is the same, all the list elements are unique.
return len(set(words)) == len(words)



In the interest of brevity I'd use sum and a comprehension to build the count rather than a named variable:


with open('file_with_phrases.txt', 'r') as file:
    print(sum(1 for line in file if has_unique_words(line)))