Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Exercise 1
Write a Python script to concatenate the following dictionaries to create a new one.
Example input 1:
dic1 = {1: 10, 2: 20}
dic2 = {3: 30, 4: 40}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40}
Example input 2:
dic1 = {1: 10, 2: 20}
dic2 = {3: 30, 4: 40}
dic3 = {5: 50, 6: 60}
Output : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
Merge dictionaries
1
2
class Solution:
def merge_dictionaries(self, dicts: list[dict[int, int]]) -> dict[int, int]:
Enter to Rename, Shift+Enter to Preview
Exercise 2
Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x).
Example input 1:
n = 1
Expected Result : {1: 1}
Example input 2:
n = 5
Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Merge dictionaries
1
2
class Solution:
def generate_squared_dict(self, n: int) -> dict[int, int]:
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content