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.

Expert's answer

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))
LATEST TUTORIALS
APPROVED BY CLIENTS