Answer:
I am writing a Python program.
def phonebook(names, numbers): #method phonebook that takes two parameters i.e a list of names and a list of phone numbers
dict={} #creates a dictionary
for x in range(len(names)): # loop through the names
dict[names[x]]=numbers[x] #maps each name to its phone number
return dict #return dictionary in key:value form i.e. name:number
#in order to check the working of this function, provide the names and numbers list and call the function as following:
names = ['Jackie', 'Joshua', 'Marguerite']
numbers = ['404-555-1234', '678-555-5678', '770-555-9012']
print(phonebook(names, numbers))
Explanation:
The program has a function phonebook() that takes two parameters, name which is a list of names and numbers that is a list of phone numbers.
It then creates a dictionary. An empty dictionary is created using curly brackets. A dictionary A dictionary is used here to maps a names (keys) phone numbers (values) in order to create an unordered list of names and corresponding numbers.
for x in range(len(names)):
The above statement has a for loop and two methods i.e. range() and len()
len method is used to return the length of the names and range method returns sequence of numbers just to iterate as an index and this loops keeps iterating until x exceeds the length of names.
dict[names[x]]=numbers[x]
The above statement maps each name to its phone number by mapping the first name with the first umber, the second name with the second number and so on. This mapping is done using x which acts like an index here to map first name to first number and so on. For example dict[names[1]]=numbers[1] will map the name (element) at 1st index of the list to the number (element) at 1st index.
return dict retursn the dictionary and the format of dictionary is key:value where key is the name and value is the corresponding number.
The screenshot of the program and its output is attached.