find all words in list/file that begin/ends with a specific prefix/suffix
<p>The below code gives the words which begin/ends with a specific prefix/suffix.</p>

<pre><code>string_list = [line.strip() for line in open("file.txt", 'r')]
for word in string_list:
    if word[-1] == "a":
        print word


string_list = [line.strip() for line in open("file.txt", 'r')]
for word in string_list:
    if word[0] == "fi":
        print word
</code></pre>

<p>How can i optimize it to be real fast on huge data.
also how can i pass the param like</p>

<pre><code>python test.py --prefix fi
python test.py --suffix o
</code></pre>

<p>Thanks in advance.</p>

<p>If <code>word</code> is a string, then <code>word[0] == "fi"</code> does not do what you think it does. </p>

<p>You can instead use <code>startswith</code> and <code>endswith</code> to check for <em>multicharacter</em> suffixes and prefixes.</p>

<pre><code>string_list = open("file.txt", 'r')

for word in string_list:
    if word.startswith("fi") or word.endswith('a'):
        print word
</code></pre>

<hr>

<p>To pass the suffix/ prefix as a parameter to your script, have a look at <a href="https://docs.python.org/3/library/argparse.html" rel="nofollow"><code>argparse</code></a></p>

python regex prefix suffix