Python Program to Create a Dictionary with a Dictionary Literal
A dictionary is a collection of key-value pairs that are unordered, changeable, and indexed in Python. In Python, you can create a dictionary using a dictionary literal. Let’s see how to create a dictionary using a dictionary literal in Python.
Dictionary Literal
A dictionary literal is a way to create a dictionary in Python by enclosing a comma-separated list of key-value pairs in curly braces {}. The syntax for creating a dictionary literal is as follows:
{key1:value1, key2:value2, key3:value3, ...}
The keys in a dictionary must be unique and immutable, whereas the values can be of any data type.
Creating a Dictionary
Let’s create a dictionary using a dictionary literal in Python.
# creating a dictionary
students = {'John': 80, 'Emma': 75, 'Peter': 90, 'Mark': 85, 'Lucas': 95}
# printing the dictionary
print(students)
Output:
{'John': 80, 'Emma': 75, 'Peter': 90, 'Mark': 85, 'Lucas': 95}
In the above example, we have created a dictionary named "students" using a dictionary literal. The keys in the dictionary are the names of the students, and the values are their corresponding marks.
Adding Items to a Dictionary
You can add items to a dictionary using the following syntax:
dict[key] = value
Let’s add a new student to the dictionary.
# adding a new student to the dictionary
students['Simon'] = 92
# printing the updated dictionary
print(students)
Output:
{'John': 80, 'Emma': 75, 'Peter': 90, 'Mark': 85, 'Lucas': 95, 'Simon': 92}
In the above example, we have added a new student named "Simon" to the dictionary with a score of 92.
Updating Items in a Dictionary
You can update an item in a dictionary using the following syntax:
dict[key] = new_value
Let’s update the score of a student in the dictionary.
# updating the score of a student
students['Peter'] = 95
# printing the updated dictionary
print(students)
Output:
{'John': 80, 'Emma': 75, 'Peter': 95, 'Mark': 85, 'Lucas': 95, 'Simon': 92}
In the above example, we have updated the score of the student named "Peter" in the dictionary to 95.
Deleting Items from a Dictionary
You can delete an item from a dictionary using the following syntax:
del dict[key]
Let’s delete a student from the dictionary.
# deleting a student from the dictionary
del students['Mark']
# printing the updated dictionary
print(students)
Output:
{'John': 80, 'Emma': 75, 'Peter': 95, 'Lucas': 95, 'Simon': 92}
In the above example, we have deleted the student named "Mark" from the dictionary.
Accessing Items in a Dictionary
You can access the items in a dictionary by referring to its key name inside square brackets [].
Let’s access the score of a student in the dictionary.
# accessing the score of a student
print(students['John'])
Output:
80
In the above example, we have accessed the score of the student named "John" in the dictionary.
Checking If a Key Exists in a Dictionary
You can check if a key exists in a dictionary using the following syntax:
if key in dict:
# do something
Let’s check if a student exists in the dictionary.
# checking if a student exists in the dictionary
if 'Lucas' in students:
print('Lucas is in the dictionary')
else:
print('Lucas is not in the dictionary')
Output:
Lucas is in the dictionary
In the above example, we have checked if the student named "Lucas" exists in the dictionary.
Looping Through a Dictionary
You can loop through the items in a dictionary using the following syntax:
for key, value in dict.items():
# do something
Let’s loop through the items in the dictionary.
# looping through the items in the dictionary
for key, value in students.items():
print(key, ':', value)
Output:
John : 80
Emma : 75
Peter : 95
Lucas : 95
Simon : 92
In the above example, we have looped through the items in the dictionary and printed them.
Conclusion
In this article, we have learned how to create a dictionary using a dictionary literal in Python. We have also learned how to add, update, delete, access, check if a key exists, and loop through the items in a dictionary. Dictionaries are essential data structures in Python, and knowing how to create and manipulate them efficiently is crucial for any Python developer.