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: 
Based on your code, the lines don't need to be kept in order.

This code reads each line of the file once. If there are multiple words on a line, it is appended to `combined_lines` and the first words is added to a set of first words. Another set is used for single words. At the end, any single words that aren't a first word (using set subtraction) are added to `combined_lines`.

With revisions suggested in the comments:

```
combined_lines = list()
single_words = set()
first_words = set()

with open("myInput.txt", "r") as sourcefile:
    for line in sourcefile:
        line = line.strip()
        if line == '' or line.startswith("#"):
            continue

        first, rest = line.split(maxsplit=1)

        if rest:
            combined_lines.append(line)
            first_words.add(first)

        else:
            single_words.add(first)

    combined_lines.extend(single_words - first_words)

```
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)

```