Learning To Use Dictionaries In Python

AI Writer
3 min readJan 13, 2023

--

Dictionaries are one of the most important data structures in Python, and they are incredibly useful for storing and manipulating data. A dictionary is a collection of key-value pairs, where each key is unique and is used to access its corresponding value. In this article, we will explore the basics of working with dictionaries in Python, including how to create, access, and manipulate them.

Creating a Dictionary

To create a dictionary in Python, you use curly braces {} and separate the keys and values with a colon : For example, the following code creates a dictionary with three key-value pairs:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict)
# Output: {'name': 'John', 'age': 30, 'city': 'New York'}

You can also create a dictionary using the built-in dict() function. For example:

my_dict = dict(name='John', age=30, city='New York')
print(my_dict)
# Output: {'name': 'John', 'age': 30, 'city': 'New York'}

Accessing Elements in a Dictionary

You can access the values within a dictionary using the keys. For example, the following code accesses the value associated with the key ‘name’:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict['name'])
# Output: 'John'

You can also use the get() method to access values. This method is useful because it returns None if the key is not found, rather than raising a KeyError. For example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.get('age'))
# Output: 30

Manipulating Dictionaries

You can add, remove, and change elements within a dictionary. To add a new key-value pair, you can use the assignment operator (=). For example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['gender'] = 'male'
print(my_dict)
# Output: {'name': 'John', 'age': 30, 'city': 'New York', 'gender': 'male'}

To remove a key-value pair, you can use the del keyword or the pop() method. For example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
del my_dict['age']
print(my_dict)
# Output: {'name': 'John', 'city': 'New York'}
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict.pop('age')
print(my_dict)
# Output: {'name': 'John', 'city': 'New York'}

To change the value associated with a key, you can use the assignment operator again. For example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['age'] = 35
print(my_dict)
#Output: {'name': 'John', 'age': 35, 'city': 'New York'}

Iterating over Dictionaries You can iterate over the keys, values, or key-value pairs of a dictionary using a for loop. For example, to iterate over the keys:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
for key in my_dict:
print(key)
# Output: 'name', 'age', 'city'

To iterate over the values:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
for value in my_dict.values():
print(value)
# Output: 'John', 30, 'New York'

To iterate over the key-value pairs:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
for key, value in my_dict.items():
print(key, value)
# Output: 'name', 'John', 'age', 30, 'city', 'New York'

In conclusion, dictionaries are a powerful data structure in Python, and they are essential for storing and manipulating data. They allow you to store data in a more organized manner, making it easier to access and manipulate. By learning how to create, access, and manipulate dictionaries, you can use them to effectively organize and store data in your Python programs.

--

--

AI Writer

I am a python programmer that is trying to help other people gain the skill of programming in python.