Answer to Question #283052 in Python for Evans Mulenga

Question #283052

Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Do not copy them from the textbook or any other source.



Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.

1
Expert's answer
2021-12-27T08:38:08-0500
dic = {'model':'BMW','color':'black','produced country':'Germany'} #example of dictinary
l = list(dic.items())  #list of key,value pairs, which are tuples
print('dic.items() = ',l) #returns collection of tuples(useful)
print('Type of dic.items() is',type(l))
for k,v in dic.items():  #loop over the dictionary using key,value tuples
    print('The car', k, 'is',v)
    


#Tuples are useful loops with zip() function
l1 = [1,2,3] 
l2 = [4,5,6] 
zip_ = list(zip(l1, l2))  #list of tuples with the A and B lists elements


print('Type of zipped elements is ',type(zip_))
for a,b in zip_: 
    print(a,b)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS