How to remove a word from str in list in dictionary

Question:

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>',]}
Asked By: adeljalalyousif

||

Answers:

You can use str.replace to replace <start>/<end> with an empty string:

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>",
    ],
}

for v in my_dict.values():
    v[:] = (s.replace("<start>", "").replace("<end>", "").strip() for s in v)

print(my_dict)

Prints:

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

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.

Answered By: NameVergessen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.