Answer to Question #100872 in Python for Olayemi Abiona

Question #100872
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.
1
Expert's answer
2020-01-03T03:11:23-0500

Hey. Task completed.


names = ("Bob", "Elena", "Mark", "Tom")
ages = (15, 14, 16, 17)

#create dictionary
dictionary = {}
for i in range(len(names)):
	dictionary[names[i]] = ages[i]

print(dictionary.items()) #use items method

#use zip and enumerate functions
for i, (val1, val2) in enumerate(zip(names, ages)):
    print(str(i+1)+ ".", val1, "-", str(val2))

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