How to iterate over a string and for each letter, if it is in a dictionary, append that word to the end of `syr_list`

Question:

I have a dictionary called to_nato that looks like this:

to_nato = {'a': 'alfa',
           'b': 'bravo',
           'c': 'charlie',
           'd': 'delta',
           'e': 'echo',
           'f': 'foxtrot',
           'g': 'golf',
           'h': 'hotel',
           'i': 'india'}

I need to write loop to iterate over a string "stateofny" and for each letter, if it is in a dictionary, append that word to the end of syr_list

I am trying this:

syr_str="stateofny"
syr_list=[]

for letter in syr_str:
    for key, value in zip(to_nato.keys(), to_nato.values()):
                if letter == key:
                   syr_list.append(value) 
print(syr_list)

but it returns empty list. What am I doing wrong?

Asked By: Anano Mikadze

||

Answers:

Your method works fine, it doesn’t return an empty list (assuming that you had a typo in your post, and your to_nato is actually a valid dictionary). That being said, that’s not how you use a dictionary. The sole purpose of a dictionary is to make it easier to randomly access elements, without using for loops and if statements.

This is a better version of your code:

for letter in syr_str:
    if letter in to_nato:
        syr_list.append(to_nato[letter])

Or, using a list comprehension:

syr_list = [to_nato[letter] for letter in syr_str if letter in to_nato]
Answered By: Selcuk

Your code works fine and returns ['alfa', 'echo', 'foxtrot']

However here’s a better version of it

to_nato = {'a': 'alfa',
       'b': 'bravo',
       'c': 'charlie',
       'd': 'delta',
       'e': 'echo',
       'f': 'foxtrot',
       'g': 'golf',
       'h': 'hotel',
       'i': 'india'}
syr_str = "stateofny"
syr_list = [to_nato.get(letter) for letter in syr_str if letter in to_nato]
print(syr_list)
Answered By: xFranko

Based on the how you are presenting your dictionary, to_nato, I am unsure if it is a list of dictionaries or a dictionary. I will provide answers to accommodate each case.

If to_nato is a dictionary:

to_nato = {'a' : 'alpha'
          ,'b': 'bravo'
          ,'c': 'charlie'
          ,'d': 'delta'
          ,'e': 'echo'
          ,'f': 'foxtrot'
          ,'g': 'golf'
          ,'h': 'hotel'
          ,'i': 'india'}

syr_str="stateofny"
syr_list=[]

for letter in syr_str:
    if letter in to_nato.keys():
        syr_list.append(to_nato[letter])
print(syr_list)

Output:

['alpha', 'echo', 'foxtrot']

If to_nato is a list of dictionaries:

to_nato = [{'a': 'alfa'}
       ,{'b': 'bravo'}
       ,{'c': 'charlie'}
       ,{'d': 'delta'}
       ,{'e': 'echo'}
       ,{'f': 'foxtrot'}
       ,{'g': 'golf'}
       ,{'h': 'hotel'}
       ,{'i': 'india'}]

syr_str="stateofny"
syr_list=[]

for letter in syr_str:
    for dictionary in to_nato:
        for key, value in dictionary.items():
            if key == letter:
                syr_list.append(value)
print(syr_list)

Output:

['alpha', 'echo', 'foxtrot']
Answered By: mgold
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.