0% found this document useful (0 votes)
14 views4 pages

Assignement (Dictionary)

Uploaded by

nipurnverma3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Assignement (Dictionary)

Uploaded by

nipurnverma3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.Write a Python script to add a key to a dictionary.

Sample Dictionary : {0: 10, 1: 20}


Expected Result : {0: 10, 1: 20, 2: 30}

2. Write a Python script to concatenate the following dictionaries to


create a new one.

Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

3. Write a Python script to check whether a given key already exists in


a dictionary.

4. Write a Python program to iterate over dictionaries using for loops.

5. Write a Python script to generate and print a dictionary that


contains a number (between 1 and n) in the form (x, x*x).
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

6. Write a Python script to print a dictionary where the keys are


numbers between 1 and 15 (both included) and the values are the
square of the keys.
Sample Dictionary
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11:
121, 12: 144, 13: 169, 14: 196, 15: 225}

7. Write a Python program to sum all the items in a dictionary.


8. Write a Python program to map two lists into a dictionary.

9. Write a Python program to sort a given dictionary by key.

10. Write a Python program to get the maximum and minimum values
of a dictionary.

11. Write a Python program to remove duplicates from the dictionary.

12. Write a Python program to check if a dictionary is empty or not.

13. Write a Python program to print all distinct values in a dictionary.


Sample Data : [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI":
"S005"}, {"VII":"S005"}, {"V":"S009"},{"VIII":"S007"}]
Expected Output : Unique Values: {'S005', 'S002', 'S007', 'S001',
'S009'}

14. Write a Python program to create a dictionary from a string.


Note: Track the count of the letters from the string.
Sample string : 'w3resource'
Expected output: {'w': 1, '3': 1, 'r': 2, 'e': 2, 's': 1, 'o': 1, 'u': 1, 'c': 1}

15. Write a Python program to remove spaces from dictionary keys.

16. Write a Python program to get the top three items in a shop.
Sample data: {'item1': 45.50, 'item2':35, 'item3': 41.30, 'item4':55,
'item5': 24}
Expected Output:
item4 55
item1 45.5
item3 41.3

17. Write a Python program to drop empty items from a given


dictionary.
Original Dictionary:
{'c1': 'Red', 'c2': 'Green', 'c3': None}
New Dictionary after dropping empty items:
{'c1': 'Red', 'c2': 'Green'}

18. Write a Python program to extract a list of values from a given list
of dictionaries.
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92,
'Science': 88}]
Extract a list of values from said list of dictionaries where subject =
Science
[92, 94, 88]
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92,
'Science': 88}]
Extract a list of values from said list of dictionaries where subject =
Math
[90, 89, 92]

19. Write a Python program to access dictionary key's element by


index.
Expected Output:
physics
math
chemistry
20. Write a Python program to filter even numbers from a dictionary of
values.
Original Dictionary:
{'V': [1, 4, 6, 10], 'VI': [1, 4, 12], 'VII': [1, 3, 8]}
Filter even numbers from said dictionary values:
{'V': [4, 6, 10], 'VI': [4, 12], 'VII': [8]}
Original Dictionary:
{'V': [1, 3, 5], 'VI': [1, 5], 'VII': [2, 7, 9]}
Filter even numbers from said dictionary values:
{'V': [], 'VI': [], 'VII': [2]}

You might also like