Practice Top 30 Python Programming Questions on Dictionaries Data Structure
Q1. Write a program to create a dictionary.
Input:
{'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Q2. Write a program to access a value from a dictionary using its key.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Key: 'age'
Expected Output:
Age: 25
Q3. Write a program to add a new key-value pair to a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25}
Key: 'city', Value: 'New York'
Expected Output:
Updated Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Q4. Write a program to remove a key-value pair from a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Key to remove: 'age'
Expected Output:
Updated Dictionary: {'name': 'Alice', 'city': 'New York'}
Q5. Write a program to check if a key exists in a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Key to check: 'city'
Expected Output:
Key 'city' exists in the dictionary.
Q6. Write a program to get all the keys in a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Keys: ['name', 'age', 'city']
Q7. Write a program to get all the values in a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Values: ['Alice', 25, 'New York']
Q8. Write a program to update the value of an existing key in a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25}
Key: 'age', New Value: 26
Expected Output:
Updated Dictionary: {'name': 'Alice', 'age': 26}
Q9. Write a program to merge two dictionaries.
Input:
Dictionary 1: {'name': 'Alice', 'age': 25}
Dictionary 2: {'city': 'New York', 'job': 'Engineer'}
Expected Output:
Merged Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York', 'job': 'Engineer'}
Q10. Write a program to get the length (number of items) of a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Length of Dictionary: 3
Q11. Write a program to create a dictionary with default values.
Input:
Dictionary: {'name': 'Alice', 'age': 25}
Key: 'city', Default Value: 'Unknown'
Expected Output:
City: Unknown
Q12. Write a program to create a nested dictionary.
Input:
Dictionary: {'name': 'Alice', 'address': {'city': 'New York', 'zipcode': '10001'}}
Expected Output:
Nested Dictionary: {'name': 'Alice', 'address': {'city': 'New York', 'zipcode': '10001'}}
Q13. Write a program to iterate through a dictionary and print its keys and values.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Key: name, Value: Alice
Key: age, Value: 25
Key: city, Value: New York
Q14. Write a program to create a dictionary where a key maps to multiple values (use a list).
Input:
Dictionary: {'fruits': ['apple', 'banana', 'cherry']}
Expected Output:
Dictionary: {'fruits': ['apple', 'banana', 'cherry']}
Q15. Write a program to find the maximum value in a dictionary.
Input:
Dictionary: {'a': 10, 'b': 20, 'c': 15}
Expected Output:
Maximum Value: 20
Q16. Write a program to find the minimum value in a dictionary.
Input:
Dictionary: {'a': 10, 'b': 20, 'c': 15}
Expected Output:
Minimum Value: 10
Q17. Write a program to get the key with the maximum value.
Input:
Dictionary: {'a': 10, 'b': 20, 'c': 15}
Expected Output:
Key with Maximum Value: b
Q18. Write a program to get the key with the minimum value.
Input:
Dictionary: {'a': 10, 'b': 20, 'c': 5}
Expected Output:
Key with Minimum Value: c
Q19. Write a program to check if a value exists in a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Value to check: 'Alice'
Expected Output:
Value 'Alice' exists in the dictionary.
Q20. Write a program to remove all items from a dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Dictionary after clear: {}
Q21. Write a program to create a dictionary from a list of tuples.
Input:
List of tuples: [('name', 'Alice'), ('age', 25), ('city', 'New York')]
Expected Output:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Q22. Write a program to convert a dictionary into a list of tuples.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
List of Tuples: [('name', 'Alice'), ('age', 25), ('city', 'New York')]
Q23. Write a program to check if a dictionary is empty.
Input:
Dictionary: {}
Expected Output:
The dictionary is empty.
Q24. Write a program to reverse a dictionary (swap keys and values).
Input:
Dictionary: {'name': 'Alice', 'age': 25}
Expected Output:
Reversed Dictionary: {'Alice': 'name', 25: 'age'}
Q25. Write a program to sort a dictionary by its keys.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Sorted Dictionary by Keys: {'age': 25, 'city': 'New York', 'name': 'Alice'}
Q26. Write a program to sort a dictionary by its values.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Sorted Dictionary by Values: {'age': 25, 'name': 'Alice', 'city': 'New York'}
Q27. Write a program to find the sum of all values in a dictionary where values are numeric.
Input:
Dictionary: {'a': 10, 'b': 20, 'c': 30}
Expected Output:
Sum of Values: 60
Q28. Write a program to create a dictionary using the fromkeys() method, where all values are initialized to the same value.
Input:
Keys: ['name', 'age', 'city']
Value: 'Unknown'
Expected Output:
Dictionary: {'name': 'Unknown', 'age': 'Unknown', 'city': 'Unknown'}
Q29. Write a program to copy a dictionary to another dictionary.
Input:
Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Expected Output:
Copied Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Q30. Write a program to get the key-value pair with the maximum value from a dictionary.
Input:
Dictionary: {'a': 10, 'b': 20, 'c': 30}
Expected Output:
Item with Maximum Value: ('c', 30)
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!