lundi 15 juillet 2019

Want create a new list from existing list using if else statement

I want to create a new list using existing list "dta", where i do not want a value for "banana" but a numeric for others. desired list is below:

[1,3,4,5,6]

but when i am trying to print of my final list "d" then i get only single value.

dta=list(["apple","banana","pine","cucumber","Guava","Coconut"])

d=[]
def cont_list(x):

for i in x:
    if i=="banana":
        continue
if i=="pine":
    d.append(3)

elif i=="apple":
    d.append(1)

elif i=="cucumber":
    d.append(4)
elif i=="Guava":
    d.append(5)
else:
    d.append(6)

return d   

cont_list(dta)
print(d)

Aucun commentaire:

Enregistrer un commentaire