Question: Assume a multi-line file (`myInput.txt`) with lines comprising one or more words.

```
echo -e "Foo\nFoo bar\nFoo baz\nFoo bar baz\nBar\nBaz\nBaz qux\nBaz qux quux\nQux quux" > myInput.txt

```

I wish to remove all one-word lines that are identical with the first word of any multi-word lines **using Python**.

```
echo -e "Foo bar\nFoo baz\nFoo bar baz\nBar\nBaz qux\nBaz qux quux\nQux quux" > myGoal.txt

```

The following code satisfies my goal but does not appear overtly Pythonic to me.

```
from itertools import compress

myList = list()
with open("myInput.txt", "r") as myInput:
    for line in [l.strip() for l in myInput.readlines()]:
        if not line.startswith("#"):
            myList.append(line)

# Get all lines with more than one word
more_than_one = list(compress(myList, [len(e.strip().split(" "))>1 for e in myList]))
# Get all lines with only one word
only_one_word = list(compress(myList, [len(e.strip().split(" "))==1 for e in myList]))
# Keep only unique set of initial words of more_than_one
unique_first_words = list(set([e.split(" ")[0] for e in more_than_one]))
# Remove all union set words from only_one_word
only_one_word_reduced = [e for e in only_one_word if e not in unique_first_words]
# Combine more_than_one and only_one_word_reduced
combined_list = only_one_word_reduced + more_than_one

```

Do you have any suggestions on making the Python code slimmer and more straightforward?

Answer: 
```py
# Get all lines with more than one word
more_than_one = list(compress(myList, [len(e.strip().split(" "))>1 for e in myList]))

```

* The function `split` splits by whitespaces by default, there is no need to pass the explicit argument
* Lines have been stripped already when reading the file, using `strip` seems unnecessary
* Using list comprehension should be enough, without the function `compress` and `list`

For example:

```py
more_than_one = [line for line in myList if len(line.split()) > 1]

```

Or:

```py
more_than_one = [line for line in myList if ' ' in line]

```

* Same observations for `Get all lines with only one word`

---

```py
# Keep only unique set of initial words of more_than_one
unique_first_words = list(set([e.split(" ")[0] for e in more_than_one]))

```

* Not sure why the `set` is converted to a `list`. Checking for unique words in a set is much faster than in a list

---

* **Functions**: would be better to include all the operations in a function, for easier testing and reuse
* **PEP 8**: function and variable names should be lowercase, with words separated by underscores as necessary to improve readability. `myList` should be `my_list`. [More info](https://www.python.org/dev/peps/pep-0008/#function-and-variable-names).

---

A function that does the same could be the following:

```
def remove_duplicates(lines):
    seen = set()
    for line in lines:
        first_word, *tail = line.split()
        if tail:
            seen.add(first_word)
    return [line for line in lines if line not in seen]

```

An use it like:

```
print(remove_duplicates(['a','a b','b','c','c d','z']))
# Output: ['a b', 'b', 'c d', 'z'] 

```
Although you have several useful suggestions already, they don't quite cut to
the heart of the matter. Your example fits into a common type of problem: read
a text file; parse it to extract information; perform some computations on that
information; and report to the user. In my experience one of the best things
you can do to keep this type of code fairly simple and easy to maintain is to
**convert to meaningful information early in the process**. Here's one way to
do that for your use case:

```
# Use fileinput, so we don't have to hard code the file
# path in the script. This is handy for development/debugging.

import fileinput

# Define a meaningful object to hold information about each line.
# You could use a namedtuple, a dataclass, or an attrs class.

from collections import namedtuple

WordLine = namedtuple('WordLine', 'line first rest')

# Convert the raw information (lines) to those meaningful objects.

wlines = []
for line in fileinput.input():
    first, *rest = line.split()
    wlines.append(WordLine(line, first, rest))

```

From that point forward, the algorithmic computations become so simple that
they don't amount to much. A few examples:

```
more_than_one = [wl for wl in wlines if wl.rest]
only_one_word = [wl for wl in wlines if not wl.rest]
unique_first_words = set(wl.first for wl in wlines)

```