I am writing a function that takes in an argument. From that argument, I want to compare it to a dictionary’s set of keys and return the key’s value for any matches. So far I have been able to only return the argument matches for the keys.
def func(str):
a = []
b = {'a':'b','c':'d','e':'f'}
for i in str:
if i in b.keys():
a.append(i)
return a
Output sample:
func(‘abcdefghiabcdefghi’)
[‘a’,’c’,’e’,’a’,’c’,’e’]
Wanted output:
[‘b’,’d’,’f’,’b’,’d’,’f’]
Best not to use
stras a variable name. I think your function can be written more simply like thisIf you don’t want to use a list comprehension, then you can fix it like this