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 a word from dictionary

I want to remove all <start> and <end> from a dictionary as:

my_dict = {1:['<start> the woman is slicing onions <end>', 
       '<start> a woman slices a piece of onion with a knife <end>', 
       '<start> a woman is chopping an onion <end>', 
       '<start> a woman is slicing an onion <end>', 
       '<start> a woman is chopping onions <end>', 
       '<start> a woman is slicing onions <end>', 
       '<start> a girl is cutting an onion <end>'],
      2: ['<start> a large cat is watching and sniffing a spider <end>',
            '<start> a cat sniffs a bug <end>', 
            '<start> a cat is sniffing a bug <end>',
            '<start> a cat is intently watching an insect crawl across the floor <end>',, 
            '<start> the cat checked out the bug on the ground <end>', 
            '<start> the cat is watching a bug <end>', 
            '<start> a cat is looking at an insect <end>']
      3:['<start> a man is playing a ukulele <end>',
         '<start> a man is playing a guitar <end>', 
         '<start> a person is playing a guitar <end>',]}

>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

try:

my_dict = {k : [s.replace("<start>", "").replace("<end>", "") for s in l] for k, l in my_dict.items() }

You may want to add a strip() after the second replace to get rid of the whitespace.

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