Program Screenshots:
Sample output:
Code to Copy:
# Use python 3.5 or 3.6
# indentation of the program should be same as provided in the above screenshot:
# declare the dictionary
wwII_battle_winners =
{'india':'winner1','china':'winner3','russia':'winner2'}
# assign keys to variable battles.
battles = wwII_battle_winners.keys()
# sort the keys
battles=sorted(battles)
# Display the keys on console.
print (battles)
battles = list(wwII_battle_winners.keys()) battles.sort()
wwII_battle_winners = { "b1": "v1", "b5": "v2", "b2": "v3", "b9": "v4", } battles = list(wwII_battle_winners.keys()) battles.sort() print(battles)
SOURCE CODE:
def remover(d,lst):
for i in lst:
d.pop(i,None)
return d
Given dictionaries, d1 and d2, create a new dictionary with the following property: for each entry (a, b) in d1, if there is an entry (b, c) in d2, then the entry (a, c) should be added to the new dictionary. For example, if d1 is {2:3, 8:19, 6:4, 5:12} and d2 is {2:5, 4:3, 3:9}, then the new dictionary should be {2:9, 6:3} Associate the new dictionary with the variable d3
Phython read_file (fp) D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries: - Parameters: file pointer (fp) - Returns : 3 dictionaries of list of tuples - The function displays nothing b) You must use the csv.reader because some fields have commas in them. c) Each row of the file contains the name of a video game, the platform which it was released, the release year, the genre (i.e. shooter, puzzle, rpg, fighter, etc.), the publishing...
Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys that are found in both dictionaries and returns a new dictionary that contains key:value pairs where the key in the new dictionary is the key found in both dictionaries, dictionl and diction2, and the new key's value being a list of the values of dictionl.key and diction2.key values concatenated together. Assumptions 1) 2) Both dictionaries can be empty and an empty dictionary is returned...
1)Assume there is a variable, nobel_peace_prizes , that is associated with a dictionary that maps years to winners of the Nobel Peace Prize for that year and assume itis up-to-date through the year 2005. Write a statement that adds an entry that maps the key 2006 to "Muhammad Yunus and Grameen Bank" .2)Given a variable, us_cabinet , that is associated with a dictionary that maps department names to department heads, replace the value "Gonzalez" with "Mukasey" for thekey "Justice Department"3)The...
Updating a Dictionary When working with lists, you probably noticed that you can change the value at a given index with a simple assignment statement. So, for example, the following code replaces the first element of lst with 42: lst[0] = 42 You can update the value for a given key in a dictionary in the same way. The general syntax for this is: d[key] = value This is also how you insert new key/value pairs to a dictionary; if...
Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys() member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict....
Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...
1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...
Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a list of strings. All of the strings in the list are keys to the dictionary. Complete the function such that it returns the subset of the dictionary defined by the keys in the list of strings. For example, with the dictionary ("puffin": 5, "corgi": 2, "three": 3) and the list ("three", "corgi"), your function should return {"corgi": 2, "three"...
Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings as a parameter. The strings are intended to represent a person's first and last name (with a blank in between). Assume the last names are unique. The function should return a dictionary (dict) whose keys are the people's last names, and whose values are their first names. For example: >>> dictionary = names([ 'Ljubomir Perkovic', \ 'Amber Settle', 'Steve Jost']) >>> dictionary['Settle'] 'Amber' >>>...