Creat Your Own


#1

I am using dictionary this this exercise. It has taken awhile but it works. It is basic as you can see.
The question is if I had 500 items, say, in the dictionary, this will loop 500 times. Could you please let me know how to improve on the code below. Cheers


ddict ={"bottleneck": "cetacea", "Clymene": "stenella", "frontlis":"stenella", "sinus":"phocoena"}

print "A non-knowledge of dolphins .."
#attempt at converting a dictionary to list  
mylist=[]

for key in ddict:
    if ddict[key] != "cetacea":
        mylist.append(key) 
        mylist.append(" : ") 
        mylist.append(ddict[key])
#line 13 enables correct syntax ie spacing around the puntuation coma
        mylist.append(", ")
    else:
        print "Dolphins"
#print " " .join(map(str, mylist))

del mylist[len(mylist) -1] # this isto remove the coma at the end of the iteration

#print " " .join(map(str, mylist))

del mylist[len(mylist)-4]
#print " " .join(map(str, mylist))

mylist.insert(len(mylist)-3," and ") 

# the next line does makes it possible that quotationmarks, " ", are not printed. They are still there!!
print "" .join(map(str, mylist)), "are beautiful creatures"

    A non-knowledge of dolphins ..
Dolphins
Clymene : stenella, frontlis : stenella and sinus : phocoena are beautiful creatures
None.


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.