What is dictionary in Python, syntax and example?

A dictionary in Python is a data structure that stores data in key-value pairs, similar to JSON format.
A dictionary in Python is a way to store information in pairs, where each item has a unique "key" and a "value" attached to it.

To make a dictionary in Python, we use {} braces, where data is stored as {key: value} pairs.

syntax:

dictionary_name = {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
}

Example of dictionary:

student = {
    "Name": "kriss moris",
    "Age": 20,
    "Email": "[email protected]"
}

print(student)

Output:

{'Name': 'kriss moris', 'Age': 20, 'Email': '[email protected]'}

How to check the datatype of a dictionary?

To check the datatype you can use type() method.

student = {
    "Name": "kriss moris",
    "Age": 20,
    "Email": "[email protected]"
}

print(type(student))

Output:

<class 'dict'>

 

How do you access or retrieve data from a dictionary in Python?

 syntax:

dictionary_name["key"]

Example of dictionary:

student = {
    "Name": "kriss moris",
    "Age": 20,
    "Email": "[email protected]"
}

print(student["Name"])
print(student["Email"])

Output:

kriss moris
[email protected]

 


Unique and important Python dictionary knowledgable questions

 

Question 1. How can you check if a key exists in a dictionary?

Answer: You can use the in keyword to check if a key exists in the dictionary.

Example:

data = {"name": "theagleye", "age": 25}
print("name" in data) 
print("address" in data) 

Output:

True
False

 

Question 2. How do you get all the keys in a dictionary?

Answer: Use the keys() method to retrieve all keys in a dictionary.

Example:

data = {"name": "Alice", "age": 25}
print(data.keys()) 

Output:

dict_keys(['name', 'age'])

 

 Question 3. How can you retrieve all the values in a dictionary?

Answer: Use the values() method to get all values in a dictionary.

Example:

data = {"name": "Alice", "age": 25}
print(data.values()) 

Output:

dict_values(['Alice', 25])

 

 Question 4. How do you get both keys and values in a dictionary?

Answer: The items() method returns a view object that displays a list of dictionary's key-value pairs.

Example:

data = {"name": "Alice", "age": 25}
print(data.items())

Output:

dict_items([('name', 'Alice'), ('age', 25)])

 

 Question 5. How can you safely retrieve a value for a key, providing a default if the key doesn’t exist?

Answer: Use the get() method, passing a default value.

Example:

data = {"name": "Alice", "age": 25}
print(data.get("age", "Not Found"))  
print(data.get("address", "Not Found"))  

Output:

25
Not Found

 

 Question 6. How do you find the number of items in a dictionary?

Answer: Use the len() function to count items in a dictionary.

Example:

data = {"name": "Alice", "age": 25}
print(len(data)) 

Output:

2

 

 Question 7. How can you merge two dictionaries?

Answer: From Python 3.9+, use the | operator. Alternatively, use the update() method.

Example:

data1 = {"name": "Alice"}
data2 = {"age": 25}
merged_data = data1 | data2
print(merged_data)

Output:

{'name': 'Alice', 'age': 25}

 

 Question 8. How do you create a dictionary from two lists of keys and values?

Answer: Use the zip() function with dict().

Example:

keys = ["name", "age"]
values = ["Alice", 25]
data = dict(zip(keys, values))
print(data) 

Output:

{'name': 'Alice', 'age': 25}

 

 Question 9. How can you get a dictionary's key with the highest value?

Answer: Use the max() function with the get() method.

Example:

data = {"a": 3, "b": 5, "c": 1}
highest_key = max(data, key=data.get)
print(highest_key) 

Output:

b

 

 Question 10. How can you create a dictionary with default values for a list of keys?

Answer: Use fromkeys() to create a dictionary with default values.

Example:

keys = ["a", "b", "c"]
data = dict.fromkeys(keys, 0)
print(data)  

Output:

{'a': 0, 'b': 0, 'c': 0}

 

 Question 11. How can you update a dictionary without modifying the original?

Answer: Create a copy with copy() and then update it.

Example:

data = {"a": 1, "b": 2}
new_data = data.copy()
new_data["c"] = 3
print(new_data) 
print(data)  

Output:

{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2}

 

 Question 12. How do you filter a dictionary based on condition(s) on values?

Answer: Use dictionary comprehension.

Example:

data = {"a": 3, "b": 5, "c": 1}
filtered_data = {k: v for k, v in data.items() if v > 2}
print(filtered_data) 

Output:

{'a': 3, 'b': 5}

 

 Question 13. How can you sort a dictionary by keys?

Answer: Use the sorted() function with dict().

Example:

data = {"b": 2, "a": 1, "c": 3}
sorted_data = dict(sorted(data.items()))
print(sorted_data) 

Output:

{'a': 1, 'b': 2, 'c': 3}

 

 Question 14. How can you sort a dictionary by values?

Answer: Use the sorted() function with lambda.

Example:

data = {"a": 3, "b": 1, "c": 2}
sorted_data = dict(sorted(data.items(), key=lambda item: item[1]))
print(sorted_data)  

Output:

{'b': 1, 'c': 2, 'a': 3}

 

 Question 15. How do you convert dictionary keys into a list?

Answer: Use list() with keys().

Example:

data = {"name": "Alice", "age": 25}
keys_list = list(data.keys())
print(keys_list) 

Output:

['name', 'age']

 

 Question 16. How do you convert dictionary values into a list?

Answer: Use list() with values().

Example:

data = {"name": "Alice", "age": 25}
values_list = list(data.values())
print(values_list)  

Output:

['Alice', 25]

 

 Question 17. How can you find the sum of all values in a dictionary?

Answer: Use sum() on values().

Example:

data = {"a": 3, "b": 5, "c": 1}
sum_values = sum(data.values())
print(sum_values) 

Output:

9

 

 Question 18. How do you get a dictionary view of keys and values together?

Answer: Use the items() method.

Example:

data = {"name": "Alice", "age": 25}
print(data.items())  

Output:

dict_items([('name', 'Alice'), ('age', 25)])

 

 Question 19. How do you clear all items in a dictionary?

Answer: Use the clear() method.

Example:

data = {"name": "Alice", "age": 25}
data.clear()
print(data)

Output:

{}

 

 Question 20. How can you copy a dictionary?

Answer: Use the copy() method to make a shallow copy.

Example:

data = {"name": "Alice", "age": 25}
data_copy = data.copy()
print(data_copy) 

Output:

{'name': 'Alice', 'age': 25}

 

 Question 21. How can you remove a key from a dictionary and get its value at the same time?

Answer: Use the pop() method.

Example:

data = {"name": "Alice", "age": 25}
age = data.pop("age")
print(age)
print(data)  

Output:

25
{'name': 'Alice'}

 

Leave a comment

You must be logged in to post a comment.

0 Comments