I'm trying to separate the flowers and the shrubs from the given data to two separate lists. 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"]
flowers = []
shrubs = []
for i in data:
x=i.split("-")
print(x)
if "Shrub" in x:
shrubs.extend(x)
elif "Flower" in x:
flowers.extend(x)
print(shrubs)
print(flowers)
The lists shrubs and flowers show out to be empty. Why??
x."-", but those strings also have spaces. So the first string will give you["Andromeda ", " Shrub"]."Shrub"is not in that list. Try splitting on" - "instead.