making lists
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”,
]
Answers ( 3 )
Ans. flowers = []
shrubs = []
for plant in data:
if “Flower” in plant:
flowers.append(plant)
elif “Shrub” in plant:
shrubs.append(plant)
shrubs = [x for x in data if x.find(“Shrub”) > 0]
flowers = [x for x in data if x.find(“Flower”) > 0]
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)