Answer to Question #283503 in Python for kika

Question #283503

Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples.


1
Expert's answer
2021-12-29T07:57:26-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