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 use removesuffix() within this for loop to return the list items without the trailing words I chose?

I am tasked with taking the following list, and obtain two different lists based on whether the main one contains " – Flowers" or " – Shrub" in their items. I did this, both with list comprehensions and with for loops. However, I can’t seem to be able to remove these trailing parts and print the lists without them using the removesuffix() function.

Here is my code

data = [
    "Andromeda - Shrub",
    "Bellflower - Flower",
    "China Pink - Flower",
    "Daffodil - Flower",
    "Evening Primrose - Flower",
    "French Marigold - Flower",
    "Hydrangea - Shrub",
    "Iris - Flower",
    "Japanese Camellia - Shrub",
    "Lavender - Shrub",
    "Lilac - Shrub",
    "Magnolia - Shrub",
    "Peony - Shrub",
    "Queen Anne's Lace - Flower",
    "Red Hot Poker - Flower",
    "Snapdragon - Flower",
    "Sunflower - Flower",
    "Tiger Lily - Flower",
    "Witch Hazel - Shrub",
]

#List comprehension method
# flowers = [flowers for flowers in data if "Flower" in flowers]
# shrubs = [shrubs for shrubs in data if "Shrub" in shrubs]

#For loops method
flowers = []
shrubs = []

for strings in data:
    if " - Flower" in strings:
        trailing = strings
        trailing.removesuffix(" - Flower")
        flowers.append(trailing)
    elif " - Shrub" in strings:
        strings.removesuffix(" - Shrub")
        shrubs.append(strings)


print(flowers)
print(f"\n")
print(shrubs)

The code works, but I alwyas obtain the result as if the removal doesn’t happen:

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

['Bellflower - Flower', 'China Pink - Flower', 'Daffodil - Flower', 'Evening Primrose - Flower', 'French Marigold - Flower', 'Iris - Flower', "Queen Anne's Lace - Flower", 'Red Hot Poker - Flower', 'Snapdragon - Flower', 'Sunflower - Flower', 'Tiger Lily - Flower']

How can I use the removesuffix() in both the list comprehension and for loop methods to remove those parts?

>Solution :

You need to assign the output of the removesuffix function call:

for string in data:
    if " - Flower" in string:
        trailing = string.removesuffix(" - Flower")
        flowers.append(trailing)
    elif " - Shrub" in string:
        trailing = string.removesuffix(" - Shrub")
        shrubs.append(trailing)

althought in this scenario, I would recommend you use endswith for the if statements:

flower_suffix = " - Flower"
shrub_suffix = " - Shrub"

for string in data:
    if string.endswith(flower_suffix):
        flowers.append(string.removesuffix(flower_suffix))
    elif string.endswith(shrub_suffix):
        shrubs.append(string.removesuffix(shrub_suffix))
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