Using numbers in a list to access items in a dictionary in python
<p>I have the items:</p>

<pre><code>my_list = [18, 15, 22, 22, 25, 10, 7, 25, 2, 22, 14, 10, 27]
</code></pre>

<p>in a list and I would like to use these to access items inside my dictionary by using a 'for loop', I will then append these items to a new list. My dictionary looks like this:</p>

<pre><code>my_dict = {1:"A", 2:"B", 3:"C" ... 26:"Z", 27:"_"}
</code></pre>

<p>How would I got about doing this? I have tried loops like this:</p>

<pre><code>for i in my_list:
    my_newlist = []
    letters = number_value[keys.index(i)]
    letters.append(my_newlist)
</code></pre>

<p>However I am getting errors in doing so. Cheers.</p>

<p>In another area of my code I have the revers function, in which I get the key by searching for the item with:</p>

<pre><code>for i in my_list:
    third_list = []
    num_val = keys[values.index(i)] + key_shift
    third_list.append(num_val)
    print(num_val)
</code></pre>

<p>It's being used in a substitution cypher code. The sample above works, it looks in the dictionary for the key which corresponds to "A" and stores the key in a list.</p>

<p>Encrypter is now working. You can find a version <a href="https://github.com/Bluestormy/Python/blob/master/Python%20Encrypter.py" rel="nofollow">here</a></p>

<p>Others have pointed out the inconsistencies in your code sample, so blithely ignoring those :)</p>

<pre><code>my_list = [18, 15, 22, 22, 25, 10, 7, 25, 2, 22, 14, 10, 27]
my_dict = {0: '@', 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z', 27: '_'}
my_newlist = []
for i in my_list:
    letters = my_dict[i]
    my_newlist.append(letters)

my_newlist
['R', 'O', 'V', 'V', 'Y', 'J', 'G', 'Y', 'B', 'V', 'N', 'J', '_']

#Now reverse the operation to build third_list

third_list = []
for i in my_newlist:
    key = my_dict.keys()[my_dict.values().index(i)]
    third_list.append(key)

third_list
[18, 15, 22, 22, 25, 10, 7, 25, 2, 22, 14, 10, 27]
</code></pre>

python list dictionary