making lists

Question

Q1.   The below list contains flowers and shrubs

We must make two separate lists one called flowers will only contain flower and                     the other called shrubs should only contain shrub .

Write the code to do the above mentioned.

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”,

]

in progress 0
Aman rathore 55 years 3 Answers 656 views Member 1

Answers ( 3 )

  1. Ans. flowers = []
    shrubs = []
    for plant in data:
    if “Flower” in plant:
    flowers.append(plant)
    elif “Shrub” in plant:
    shrubs.append(plant)

  2. shrubs = [x for x in data if x.find(“Shrub”) > 0]
    flowers = [x for x in data if x.find(“Flower”) > 0]

  3. 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” ]

    if ‘ Shrub’ in data:
    shrubs.append(i)
    elif ‘ Flower’ in data:
    flowers.append(i)
    else:
    pass
    print(shrubs)
    print(flowers)

Leave an answer

Browse
Browse