Kyle
Kyle

Reputation: 23

Using numbers in a list to access items in a dictionary in python

I have the items:

my_list = [18, 15, 22, 22, 25, 10, 7, 25, 2, 22, 14, 10, 27]

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:

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

How would I got about doing this? I have tried loops like this:

for i in my_list:
    my_newlist = []
    letters = number_value[keys.index(i)]
    letters.append(my_newlist)

However I am getting errors in doing so. Cheers.

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

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

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.

Upvotes: 0

Views: 121

Answers (3)

Rolf of Saxony
Rolf of Saxony

Reputation: 22453

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

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]

Upvotes: 1

LaughU
LaughU

Reputation: 253

Your code snipped does not seems to be complete because you are using the variable number_values 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 third_list and my_newlis every time

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

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

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

Upvotes: 1

jbndlr
jbndlr

Reputation: 5210

You can simply use a list comprehension where you implicitly loop through your list of numbers my_list and retrieve the value form your dict my_dict using the respective number as a key:

my_dict = {i: chr(i+64) for i in range(27)}
my_list = [1, 5, 19]

result = [my_dict[i] for i in my_list]
print(result)

yields

['A', 'E', 'S']

Make sure to check for KeyError, because your list my_list may contain numbers that are not present in your dict my_dict. If you want to avoid explicit exception handling (and silently omit missing keys), you can do something like this:

result = [my_dict[i] for i in my_list if i in my_dict.keys()]
print(result)

Upvotes: 2

Related Questions