A more pythonic way of doing the following?
<p>A more pythonic way of doing the following? In this example, I'm trying to build comments (a dict) to be key-value pairs where the key is the unmodified-version, and the value is the return value from a function where the key was passed as an arg.</p>

<pre><code>def func(word):
    return word[-1:-6:-1].upper()

subs = {
    'ALLOWED': ['one', 'two'],
    'DISALLOWED': ['three', 'four']
}

comments = {}
for sub in subs['ALLOWED']:
    # reverse and uppercase
    comments[sub] = func(sub)

print(comments)
</code></pre>

<p>Anyone have recommendation? It's not exactly important to have this done, but I love learning python idioms and ways to make my code more pythonic. Thanks!</p>

<p>Remove the loop and replace it inside the function with a dictionary comprehension. Then, you can simply return it. </p>

<p>Also, you can reverse the string <code>word</code> with <code>[::-1]</code>, not the complex <code>[-1:-6:-1]</code> slice you're using:</p>

<pre><code>def func(words):
    return {word: word[::-1].upper() for word in words} 
</code></pre>

<p>This yields the same thing:</p>

<pre><code>comments = func(subs['ALLOWED'])
print(comments)
# {'two': 'OWT', 'one': 'ENO'}
</code></pre>

python python-3.x