Python : Dictionary - Exercises and Solution


1. Write a Python script to sort (ascending and descending) a dictionary by value.

Sample Output

dictionary = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

Ascending order = [ (0, 0), (2, 1), (1, 2), (4, 3), (3, 4) ]

Descending order = {3: 4, 4: 3, 1: 2, 2: 1, 0: 0}

View Solution

2. Write a Python program to add a key to a dictionary

Sample Output

dictionary = {"Name" : "Ram" , "Age" : 23}

add_key = {"City" : "Salem"}

dictionary = {'Name' : 'Ram', 'Age' : 23, 'City' : 'Salem'}

View Solution

3. Write a Python program to concatenate following dictionaries to create a new one.

Sample Output

Dictionary 1 = {"Name" : "Ram" , "Age" : 23}

Dictionary 2 = {"City" : "Salem", "Gender" : "Male"}

Concatenate Dictionaries = {'Name' : 'Ram', 'Age' : 23, 'City' : 'Salem', 'Gender': 'Male'}

View Solution

4. Write a Python program to check whether a given key already exists in a dictionary.

Sample Output

{'Name' : 'Ram', 'Age' : 23,}

Key = Name

Key is Available in the Dictionary

View Solution

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

Sample Output

{"Name" : "Ram" , "Age" : 23 , "City" : "Salem", "Gender" : "Male"}

Name : Ram

Age : 23

City : Salem

Gender : Male

View Solution

6. 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 Output

Enter the Limit : 5

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

View Solution

7.Write a Python script to print a dictionary where the keys are numbers between 1 and n (both included) and the values are square of keys.

Sample Output

Enter the Limit : 5

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

View Solution

8.Write a Python script to merge two Python dictionaries.

Sample Output

{"Name" : "Ram" , "Age" : 23}

{"City" : "Salem", "Gender" : "Male"}

{'Name' : 'Ram', 'Age' : 23, 'City' : 'Salem', 'Gender' : 'Male'}

View Solution

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

Sample Output

{"Name" : "Ram" , "Age" : 23 , "City" : "Salem", "Gender" : "Male"}

Name : Ram

Age : 23

City : Salem

Gender : Male

View Solution

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

Sample Output

{1 : 23, 2 : 45, 3 : -17, 4 : 87}

Sum all the Items = 138

View Solution

11. Access dictionary key’s element by index.

Sample Output

{"Name" : "Pooja", "Age" : 23, "Gender" : "Female", "City" : "Salem", "Mark" : 488}

Name
Age
Gender
City
Mark

View Solution

12. Drop empty Items from a given Dictionary

Sample Output

Before Dropping = {'Name' : 'Pooja', 'Age' : 23, 'Gender' : None, 'Mark' : 488, 'City' : None}

After Dropping = {'Name' : 'Pooja', 'Age' : 23, 'Mark' : 488}

View Solution

13. Create a dictionary of keys a, b, and c

Sample Output

Dictionary = { 'A' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                          'Y' : [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
                          'Z' : [21, 22, 23, 24, 25, 26, 27, 28, 29, 30] }

A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Y = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Z = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

View Solution

14. Match key values in two dictionaries

Sample Output

A = {'Tamil' : 92, 'English' : 56, 'Maths' : 88, 'Sceince' : 97, 'Social' : 89}

B= {'Tamil' : 78, 'English' : 68, 'Maths' : 88, 'Sceince' : 97, 'Social' : 56}

Maths : 88 is present in both A and B

View Solution

15. Sort Counter by value

Sample Output

Before Sort = { 'Tamil': 92, 'Math' : 88, 'Science' : 89, 'Social' : 89, 'English' : 56 }

After Sort = ('Tamil', 92), ('Science', 89), ('Social', 89), ('Math', 88), ('English', 56)

View Solution

16. Write a Python program to count number of items in a dictionary value that is a list.

Sample Output

Dict = { 'M1' : [67,79,90,73,36], 'M2' : [89,67,84], 'M3' : [82,57] }

Number of Items in a Dictionary : 10

View Solution

17. Write a Python program to check multiple keys exists in a dictionary.

Sample Output

Dict = { 'name' : 'Ram', 'age' : 23, 'city' : 'Salem' }

Keys = {'age', 'name'}

keys exists in a dictionary : True

Keys = {'city', 'Salem'}

keys exists in a dictionary : False

View Solution

18. Write a Python program to print a dictionary line by line.

Sample Output

Dict = {  "Sam" : {"M1" : 89, "M2" : 56, "M3" : 89},
               "Suresh" : {"M1" : 49, "M2" : 96, "M3" : 89} }

Sam
M1 : 89
M2 : 56
M3 : 89
Suresh
M1 : 49
M2 : 96
M3 : 89

View Solution

19. Write a Python program to Convert two lists into a dictionary

Sample Output

keys = ["One", "Two", "Three", "Four", "Five"]

values = [1, 2, 3, 4, 5]

Convert Two List to Dict = {'One' : 1, 'Two' : 2, 'Three' : 3, 'Four' : 4, 'Five' : 5}

View Solution

20. Write a Python program to Merge two Python dictionaries into one

Sample Output

{"Name" : "Ram" , "Age" : 25 , "City" : "Salem"}

{"Gender" : "Male" , "Mark" : 422 , "Percent" : 84.4}

{'Name' : 'Ram', 'Age' : 25, 'City' : 'Salem', 'Gender' : 'Male', 'Mark' : 422, 'Percent' : 84.4}

View Solution

21. Write a Python program to Initialize dictionary with default values

Sample Output

[ "Siva", "Sam" ]

{ "designation" : "Student", "Department" : "BCA" }

{ 'Siva': {'designation' : 'Student', 'Department' : 'BCA'}, 'Sam': {'designation' : 'Student', 'Department' : 'BCA'} }

Siva : {'designation' : 'Student', 'Department' : 'BCA'}

Sam : {'designation' : 'Student', 'Department' : 'BCA'}

View Solution

22. Create a dictionary by extracting the keys from a given dictionary

Sample Output

Dictionary = {'Name' : 'Ram', 'Age' : 23, 'City' : 'Salem', 'Gender' : 'Male', 'Mark' : 450}

keys = ["Name", "City", "Mark"]

Extracting the keys from a given dictionary = {'Name' : 'Ram', 'City' : Salem, 'Mark' : 450}

View Solution

23. Write a Python program to Delete a list of keys from a dictionary

Sample Output

Dictionary = {"Name" : "Tara", "RollNo" : 130046, "Mark" : 450, "Age" : 16, "Gender" : "Female", "City" : "Salem"}

Keys to remove From a Dictionary = ["Gender", "City"]

After Delete Dictionary = {'Name' : 'Tara', 'RollNo' : 130046, 'Mark' : 458, 'Age' : 16}

View Solution

24. Write a Python program to Rename key of a dictionary

Sample Output

Before Rename Key of a Dictionary = {'Name' : 'Tara', 'RollNo' : 130046, 'Mark' : 458, 'Age' : 16}

After Rename Key of a Dictionary = {'Name' : 'Tara', 'Age' : 16, 'Mark10' : 458, 'RegNo' : 130046}

View Solution

25. Write a Python program to Replace words from Dictionary

Sample Output

String = Tutor joe's Computer Education

Replaced Strings = Tutor joe's Software Solution

View Solution

26. Write a program to print only keys of a dictionary

Sample Output

Dictionary = {"Name" : "Tara", "RollNo" : 130046, "Mark" : 450, "Age" : 16, "Gender" : "Female", "City" : "Salem"}

Dictionary Keys
Name
RollNo
Mark
Age
Gender
City

View Solution

27. Write a program to print values of dictionary.

Sample Output

Dictionary = {"Name": "Tara", "RollNo":130046, "Mark": 450, "Age":16, "Gender":"Female", "City": "Salem"}

Dictionary Values
Tara
130046
450
16
Female
Salem

View Solution

28. Write a program to sort dictionary by values (Ascending/ Descending).

Sample Output

Dictionary = { "m1" : 78 , "m2" : 89 , "m3" : 64 , "m4" : 35 , "m5" : 71 }

Ascending Order = [ ('m4', 35), ('m3', 64), ('m5', 71), ('m1', 78), ('m2', 89) ]

Descending Order = [ ('m2', 89), ('m1', 78), ('m5', 71), ('m3', 64), ('m4', 35) ]

View Solution

29. Write a program to get the maximum and minimum value of dictionary.

Sample Output

Dictionary = { "m1" : 78 , "m2" : 89 , "m3" : 64 , "m4" : 35 , "m5" : 71 }

Maximum = 89

Minimum = 35

View Solution

30. Write a program to sum all the values of a dictionary.

Sample Output

Dictionary = { "m1" : 92 , "m2" : 56 , "m3" : 88 , "m4" : 97 , "m5" : 89 }

Sum of Values = 422

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial