- Write A Function Named Create Names Dictionary Initials List Names List Which Takes A List Of Initials And A List Of 1 (35.04 KiB) Viewed 13 times
Write a function named create_names_dictionary (initials_list, names_list) which takes a list of initials and a list of
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Write a function named create_names_dictionary (initials_list, names_list) which takes a list of initials and a list of
Write a function named create_names_dictionary (initials_list, names_list) which takes a list of initials and a list of names as parameters. The function returns a dictionary by looping through each element in the first and the second list and creating a corresponding key:value item. The 'key' of each item in the dictionary is the initials of the first name and surname, and the 'value' is the name (e.g. key of 'AW", value of 'Alex Wong'). For example, the following code: names_list = ['Abby Anderson', 'Bella Campbell'] initials ['AA', 'BC'] my_dictionary = create_names_dictionary (initials, names_list) print(my_dictionary) produces {'AA': 'Abby Anderson', 'BC': 'Bella Campbell'} You can assume that both parameters are the same length and that the initials_list does not contain any duplicates. For example: Test Result names_list = ['Abby Anderson', 'Bella Campbell', 'Charlotte Donovan'] AA Abby Anderson initials = ['AA', 'BC', 'CD'] BC Bella Campbell my_dictionary = create_names_dictionary (initials, names_list) CD Charlotte Donovan for letter in sorted (my_dictionary.keys()): print(letter, my_dictionary [letter])