Pythonic way of removing one word entries from compound list Assume a multi-line file (myInput.txt) with lines comprising one or more words.
echo -e &quot;Foo\nFoo bar\nFoo baz\nFoo bar baz\nBar\nBaz\nBaz qux\nBaz qux quux\nQux quux&quot; &gt; 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 &quot;Foo bar\nFoo baz\nFoo bar baz\nBar\nBaz qux\nBaz qux quux\nQux quux&quot; &gt; myGoal.txt

The following code satisfies my goal but does not appear overtly Pythonic to me.
from itertools import compress

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

# Get all lines with more than one word
more_than_one = list(compress(myList, [len(e.strip().split(&quot; &quot;))&gt;1 for e in myList]))
# Get all lines with only one word
only_one_word = list(compress(myList, [len(e.strip().split(&quot; &quot;))==1 for e in myList]))
# Keep only unique set of initial words of more_than_one
unique_first_words = list(set([e.split(&quot; &quot;)[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?
# Get all lines with more than one word
more_than_one = list(compress(myList, [len(e.strip().split(&quot; &quot;))&gt;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:
more_than_one = [line for line in myList if len(line.split()) &gt; 1]

Or:
more_than_one = [line for line in myList if ' ' in line]


Same observations for Get all lines with only one word


# Keep only unique set of initial words of more_than_one
unique_first_words = list(set([e.split(&quot; &quot;)[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.


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']