Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to replace a certain character, being an element of a list, with another character without knowing its place in the list?

I have tried already for an hour to find a way, but I could only find codes which worked for integers to be changed.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

While I cannot read dutch, hopefully you can read English, here it goes.

It seems to me that you would like to replace a letter (thus a string) which is an element of a list. (not sure with another string or an number) so here are possible answers for you.

Say you have a list of words.

a = ["prove","road","load", "car"]

Scenerio one : You want to replace the word "car" with "dog".

[i if i !="car" else "dog" for i  in a]

My answer is to go after each members of the list until your condition is met, then replace it.

Scenerio two : You want to replace the word "car" with 10.

[i if i !="car" else 10 for i  in a]

Scenerio three : You want the character "o" with "0". (note 0 here is not an interger, but a string.)

[i.replace("o","0") for i in a]

Here you go not just after each members of the list, also each string character of the members, then replace it. Here the replace function specifies String to String only.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading