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 can i Remove the "- Flowers " and "- Shrubs" from the list before printing

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

flowers = []
shrubs = []

for plant in data:
    if "- Flower" in plant:
        flowers.append(plant)
    else:
        shrubs.append(plant)
print(flowers)
print(shrubs)

>Solution :

You could use:

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

for plant in data:
  to_append = plant.split(" - ")[0]
  if "- Flower" in plant:
    flowers.append(to_append)
  else:
    shrubs.append(to_append)

This will split the plant using " – " and make an array, then save to the to_append variable the first index of that array.
So for example if you take "Andromeda - Shrub" this will create an array {"Andromeda", "Shrub"} and its 0 index will be "Andromeda", which you then add to the new array.

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