Last Updated on April 26, 2023 by Prepbytes
Python is one of the most popular programming languages for a reason: it’s easy to learn, flexible, and powerful. One of the most important tools that every programmer should know how to use is the dictionary data structure. Here, we will discuss what a dictionary is and how to implement a dictionary program in Python.
What is a Dictionary?
A dictionary in Python is a collection of key-value pairs. The keys in a dictionary must be unique and immutable, while the values can be any data type. Dictionaries are often used to represent real-world objects, such as a person or a car. In Python, dictionaries are implemented using curly braces {} and colons :. Here is an example of a dictionary:
Creating a Dictionary in Python
To create a dictionary in Python, you simply need to define the keys and values inside curly braces {}.
Let us create a dictionary to represent a person, with the keys being "name", "age", "address", and "phone number", and the values being the actual data.
Code:
# creating dictionary person = {"name": "PrepBuddy", "age": 18, "address": "Delhi", "phone number": "5575-1234"} # printing dictionary print(person)
Output:
{'name': 'PrepBuddy', 'age': 18, 'address': 'Delhi', 'phone number': '5575-1234'}
Explanation:
In this example, we created a dictionary called "person" with the keys "name", "age", "address", and "phone number", and the values "PrepBuddy", 18, "Delhi", and "5575-1234", respectively. We then printed the dictionary using the print() function.
Accessing Values in Dictionary
To access a value in a dictionary, we can use the key as the index.
Code:
# creating dictionary person = {"name": "PrepBuddy", "age": 18, "address": "Delhi", "phone number": "5575-1234"} # Accessing the value of the "name" key print(person["name"])
Output:
PrepBuddy
Explanation:
In the above example, we accessed the value of the "name" key in the "person" dictionary by using "name" as the index. We get the output as “PrepBuddy”.
Adding Values in a Dictionary
We can add a new key-value pair to a dictionary by simply assigning a value to a new key. This will be more clear from the following code.
Code:
# creating dictionary person = {"name": "PrepBuddy", "age": 18} # printing initial dictionary print("Initial Dictionary: ", person) # Adding a new key-value pair person["address"] = "Delhi" # Printing the updated dictionary print("Updated Dictionary: ", person)
Output:
Initial Dictionary: {'name': 'PrepBuddy', 'age': 18}
Updated Dictionary: {'name': 'PrepBuddy', 'age': 18, 'address': 'Delhi'}
Explanation:
In the above code, we first created a dictionary named “person” and printed it on the screen. Then we add a new key-value pair with “address” as the key and “Delhi” as the value. Finally, we printed the updated dictionary on the screen.
Modifying Values in a Dictionary
We can also modify the values present in the dictionary. To modify the value of an existing key in a dictionary, we simply need to assign a new value to the key. Here is the code demonstrating the modification of values in a dictionary.
Code:
# creating dictionary person = {"name": "PrepBuddy", "age": 18} # printing initial dictionary print("Initial Dictionary: ", person) # Modifying dictionary person["age"] = 20 # Printing the updated dictionary print("Updated Dictionary: ", person)
Output:
Initial Dictionary: {'name': 'PrepBuddy', 'age': 18}
Updated Dictionary: {'name': 'PrepBuddy', 'age': 20}
Explanation:
In the above code, we modified the value of the “age” key from 18 to 20. This shows we can easily modify the elements of the dictionary in Python. We have printed the dictionary before and after modification to show the changes that are made.
Iterating through a Dictionary
Dictionary in Python can be iterated by using the for loop. Here is how to use the for loop for iterating over the dictionary.
Code:
# creating dictionary person = {"name": "PrepBuddy", "age": 18, "address": "Delhi"} # Iterating over dictionary for key, value in person.items(): print(key + ": " + str(value))
Output:
name: PrepBuddy
age: 18
address: Delhi
Explanation:
In the above code, we used a for loop to iterate through the "person" dictionary. We used the items() method to get a list of key-value pairs in the dictionary and then printed each key and value on a separate line.
Deleting Key-Value Pairs from a Dictionary
We can also delete a key-value pair from a dictionary in Python. For deleting key-value pairs, we use the del statement.
Code:
# creating dictionary person = {"name": "PrepBuddy", "age": 18, "address": "Delhi"} # printing initial dictionary print("Initial Dictionary: ", person) # deleting key-value pair del person["address"] # Printing the updated dictionary print("Updated Dictionary: ", person)
Output:
Initial Dictionary: {'name': 'PrepBuddy', 'age': 18, 'address': 'Delhi'}
Updated Dictionary: {'name': 'PrepBuddy', 'age': 18}
Explanation:
In this example, we used the del statement to delete the "address” key-value pair from the "person" dictionary.
Conclusion
In conclusion, Dictionaries are a powerful and versatile data structure in Python that can be used to represent complex objects and data. They are easy to create and modify and can be used in a variety of programming contexts. By using the techniques and examples presented in this article, you should be able to create your dictionary program in Python. To learn more about the Dictionary in Python, follow this link – Dictionary functions in Python.
Frequently Asked Questions(FAQs)
Here are some frequently asked questions on the Dictionary program in Python.
Ques 1. Can a dictionary have duplicate keys?
Ans. No, a dictionary cannot have duplicate keys. If we try to add a key that already exists in the dictionary, the existing value for that key will be updated with the new value.
Ques 2. Can a dictionary in Python have duplicate values?
Ans. Yes, a dictionary in Python can have duplicate values. However, each key in the dictionary must be unique.
Ques 3. What is the difference between a dictionary and a list in Python?
Ans. A dictionary in Python is a collection of key-value pairs, while a list is a collection of elements. In a list, the elements are accessed by their position (index), while in a dictionary, the elements are accessed by their key.
Ques 4. How do you get the number of items in a dictionary in Python?
Ans. To get the number of items in a dictionary in Python, you can use the len() function. For example to find the number of items in a dictionary named “person”, we can use the statement, len(person).