0

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??

2
  • 2
    Because you have spaces left, as you probably can see when you print x. Commented Jul 7, 2020 at 14:39
  • 3
    You're splitting on just "-", 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. Commented Jul 7, 2020 at 14:40

5 Answers 5

1

As Thierry Lathuille pointed out, you have whitespaces left. Try replacing all whitespaces first:

for i in data:
    i.replace(" ", "")
    x=i.split("-")
Sign up to request clarification or add additional context in comments.

Comments

1

You need to look at the second item when you split by the hyphen. You should also use the list append method over extend so you don't append the sequence of characters.

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:
   flower, flower_type = i.split("-")
   print(flower)
   if "Shrub" in flower_type:
       shrubs.append(flower)
   elif "Flower" in flower_type:
       flowers.append(flower)

print("Shrubs")
print(shrubs)
print("Flowers")
print(flowers)

It'd be better to use i.split(" - ") but you have one entry ""Lilac- Shrub" what won't match that. If that's not just a typo, fix it, then prefer using the i.split(" - ") to have cleaner strings.

Comments

1

You can use the strip method of a Python string to clean your data from unuseful spaces:

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 = [elm.strip() for elm in i.split("-")]

    print(x)

    if "Shrub" in x:
        shrubs.extend(x)
    elif "Flower" in x:
        flowers.extend(x)

print(shrubs)
print(flowers)

Comments

1

You can try

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("-")
    if "Shrub" == x[1].strip():
        shrubs.append(x[0])
    elif "Flower" == x[1].strip():
        flowers.append(x[0])
print(shrubs)
print(flowers)

Output

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

That original code didn't e

1 Comment

But using the strip method only on the second element you still have the spaces on their names...
0

You forgot the white space in the split statement:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.