3

I want to write a code that will replace certain characters in a list in an efficient way using a dictionary.

If I have:

key = {'a':'z','b':'y','c':'x'}
List = ['a','b','c']

How can I get the output

zyx

edit to clarify. The output I want is really

randomvariableorsomething = ['z', 'y', 'x'] My apologies.

Will [key[x] for x in List] work if I don't have a key for it in the dict?

3
  • Please clarify: Your output seems to be a string. What do you mean by 'replace' in this case? Commented Oct 15, 2018 at 17:32
  • What should happen for an entry in the list with no corresponding key in the dict? ['a', 'b', 'w']? Commented Oct 15, 2018 at 17:33
  • Edited for clarity. Sorry, I have to get used to specifying stuff in questions. Commented Oct 15, 2018 at 21:15

3 Answers 3

3

Use get and join:

>>> ''.join(key.get(e,'') for e in List)
'zyx'

If by 'replace' you mean to change the list to the values of the dict in the order of the elements of the original list, you can do:

>>> List[:]=[key.get(e,'') for e in List]
>>> List
['z', 'y', 'x']
Sign up to request clarification or add additional context in comments.

2 Comments

I was about to answer with key.get(e, e), so I suppose we should nail down what the asker wants in the case of a key that isn't in the dict.
Thanks for the help, I edited the vague question to be more accurate. Output = ['z','y','x']
1
key = {'a':'z','b':'y','c':'x'}
List = ['a','b','c']

print([key.get(x,"No_key") for x in List])

#### Output ####

['z', 'y', 'x']

If your interest is only to print them as string,then:

print(*[key.get(x,"No_key") for x in List],sep="")

#### Output ####

zxy

Just in case you need the solution without join.

ss = ''
def fun_str(x):
    global ss
    ss = ss + x
    return(ss)
print([fun_str(x) for x in List][-1])

#### Output ####

zxy

3 Comments

Apparaently not what OP wanted (['z', 'y', 'x'] is not zyx), and the core of the solution is the same as the accepted answer. So, what is this good for?
He has clearly mentioned as output to be in a list. And taking your comment as, feedback have edited code a bit maybe that helps.
You're right. The statement "the output I want is really" comes only after the highlighted How can I get the output zyx. Anyway, you solution is not really new - and FYI it was flagged as "low quality post". Now it is much better, I think.
-1

Both keys and List are words in python that can collide with existing objects or methods (dict.keys() and List objects), so I replaced them with k and lst respectively for best practice:

[k[x] for x in lst]

2 Comments

Neither of those identifiers shadows an existing identifier, nor are they keywords. keys is a method name of some classes, and typing.List exists, but neither of those effect this at all.
You're correct, I misspoke. Edited for clarity, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.