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>Your code snipped does not seems to be complete because you are using the variable <code>number_values</code> which is not defined in your example. 
EDIT: You still are using variables which are not defined in the code snipped you provided so I cant reproduce your error. In addition you are overwriting your <code>third_list</code> and <code>my_newlis</code> every time </p>

<p>I assume that the keys in <code>my_list</code> are also present in <code>my_dict</code> : If not do as @jbndlr told you and check for <code>KeyError</code>.</p>

<pre><code>my_list = [1, 2, 3, 26, 27]

my_dict = {1:"A", 2:"B", 3:"C" , 26:"Z", 27:"_"}

my_newlist = []
for i in my_list:
    letters = my_dict[i]
    my_newlist.append(letters)

print my_newlist
</code></pre>

<p>This yield the the items. If you are looking for something else let me know so I can adjust the answer </p>

python list dictionary