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

How to remove the first occurence of a repeated Character with python

I have been given the following string 'abcdea' and I need to find the repeated character but remove the first one so the result most be 'bcdea' I have tried to following but only get this result

def remove_rep(x):
    new_list = []
    for i in x:
        if i not in new_list:
            new_list.append(i)
    new_list = ''.join(new_list)
    print(new_list)

remove_rep('abcdea')

and the result is 'abcde' not the one that I was looking 'bcdea'

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

>Solution :

Change

new_list = ''.join(new_list)

to

new_list = ''.join(new_list[1:]+[i])

(and figure out why! Hint: what’s the condition of your if block? What are you checking for and why?)

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