Question: ```
file_str = input("Enter poem: ")
my_file = open(file_str, "r")
words = file_str.split(',' or ';')

```

I have a file on my computer that contains a really long poem, and I want to see if there are any words that are duplicated per line (hence it being split by punctuation).

I have that much, and I don't want to use a module or Counter, I would prefer to use loops. Any ideas?

Answer: 
You can use sets to track seen items and duplicates:

```
>>> words = 'the fox jumped over the lazy dog and over the bear'.split()
>>> seen = set()
>>> dups = set()
>>> for word in words:
        if word in seen:
            if word not in dups:
                print(word)
                dups.add(word)
        else:
            seen.add(word)

the
over

```
For this kinda file;

```
A hearth came to us from your hearth
foreign hairs with hearth are same are hairs

```

This will check `whole` poem;

```
lst = []
with open ("coz.txt") as f:
    for line in f:
        for word in line.split(): #splited by gaps (space)
            if word not in lst:
                lst.append(word)
            else:
                print (word)

```

Output:

```
>>> 
hearth
hearth
are
hairs
>>> 

```

As you see there are two `hearth` here, because in whole poem there are 3 `hearth`.

For check line by line;

```
lst = []
lst2 = []
with open ("coz.txt") as f:
    for line in f:
        for word in line.split():
            lst2.append(word)
            for x in lst2:
                if x not in lst:
                    lst.append(x)
                    lst2.remove(x)
print (set(lst2))

>>> 
{'hearth', 'are', 'hairs'}
>>> 

```