I'm not sure why you are limiting the numInsults either, if you want to check the whole file, if the number of lines is greater than 1K.
def checkInsultsFile(file):
with open(file, 'r') as f:
lines = [line.strip() for line in f] #puts whole file into list if it's not too large for your RAM
check = set(lines)
if len(lines) == len(check):
return False
elif len(check) < len(lines):
return True
checkInsultsFile("Insults.txt")
Alternative (run through file line by line):
def checkInsultsFile(file):
lines = []
with open(file, 'r') as f:
for line in f:
lines.append(line.strip())
check = set(lines)
if len(lines) == len(check):
return False
elif len(check) < len(lines):
return True
checkInsultsFile("Insults.txt")
This function will take all the lines in Insults.txt into a list. 'Check' is a set, which will only keep unique items in the 'lines' list. If the lines list is equal to the check list, there are no duplicates, and return False. If the check list is smaller than the lines list, you know there were duplicates, and will return True.
Alternatively, you can use bash (don't know your OS). Just to point out there are faster/simpler ways to do this, unless your python script will utilize the unique list of insults from the file in other ways:
sort Insults.txt | uniq -c
This is similar to what you can do with Counter from collections in Python, which will give you a count of all the lines in the file.