Python Dictionary! Begin Like a Pro🔥

Yashuv Baskota
3 min readJan 4, 2023

— Your Python Dictionary Guide

Introduction to Python Dictionaries

A Python dictionary is a collection of key-value pairs. It is a built-in data type in Python and is used to store and retrieve values based on a key. Dictionaries are unordered, meaning that the items in a dictionary do not have a specific order.

Here are some key features of dictionaries in Python:

  • Dictionaries are composed of key-value pairs. You can access a value by its key using the syntax my_dict[key].
  • Dictionaries are unordered. The items in a dictionary do not have a specific order.
  • Dictionaries are a built-in data type in Python. You do not need to install any additional libraries to use dictionaries.

Dictionaries are useful for storing and manipulating data in Python. They are especially useful when you need to store data that is related to a specific entity, such as a person’s name, age, and country.

Creating and Using a Dictionary

To create a dictionary in Python, you can use the following syntax:

my_dict = {}

This creates an empty dictionary. You can then add key-value pairs to the dictionary using the syntax my_dict[key] = value. For example:

# create an empty dictionary
my_dict = {}

# add key-value pairs to the dictionary
my_dict['name'] = 'James'
my_dict['age'] = 30
my_dict['country'] = 'Nepal'

# print the dictionary
print(my_dict) # Output: {'name': 'James', 'age': 30, 'country': 'Nepal'}

You can access a value by its key using the syntax my_dict[key]. For example:

print(my_dict['name'])  # Output: James

You can update a value by assigning a new value to its key. For example:

# update a value by its key
my_dict['age'] = 31
print(my_dict) # Output: {'name': 'James', 'age': 31, 'country': 'Nepal'}

You can delete a key-value pair using the del statement. For example:

# delete a key-value pair
del my_dict['country']
print(my_dict) # Output: {'name': 'James', 'age': 31}

You can check if a key exists in the dictionary using the in operator. For example:

# check if a key exists in the dictionary
print('name' in my_dict) # Output: True
print('country' in my_dict) # Output: False

Iterating Over a Dictionary

To iterate over the keys, values, or key-value pairs in a dictionary, you can use a for loop. Here are some examples:

# create a dictionary with initial key-value pairs
my_dict = {'name': 'James', 'age': 30, 'country': 'Nepal'}

# iterate over the keys in the dictionary
for key in my_dict:
print(key) # Output: name, age, country

# iterate over the values in the dictionary
for value in my_dict.values():
print(value) # Output: James, 30, Nepal

# iterate over the key-value pairs in the dictionary
for key, value in my_dict.items():
print(key, value) # Output: name James, age 30, country Nepal

You can also use the len() function to get the length of a dictionary, the keys() method to get the keys as a list, and the values() method to get the values as a list. Here are some examples:

# get the length of the dictionary
print(len(my_dict)) # Output: 3

# get the keys of the dictionary as a list
keys = list(my_dict.keys())
print(keys) # Output: ['name', 'age', 'country']

# get the values of the dictionary as a list
values = list(my_dict.values())
print(values) # Output: ['John', 30, 'Nepal']

Merging Dictionaries

To merge two dictionaries in Python, you can use the update() method. This method adds the key-value pairs of one dictionary to another dictionary.

Here is an example:

dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

# merge dict2 into dict1
dict1.update(dict2)
print(dict1) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Note that if the same key exists in both dictionaries, the value from the second dictionary will overwrite the value from the first dictionary.

Conclusion

In this article, we have covered the basics of Python dictionaries. We have learned about their key features and use cases, and we have seen examples of how to create and use dictionaries in Python. We have also learned how to iterate over the keys, values, and key-value pairs in a dictionary, and how to merge two dictionaries.

To continue learning about dictionaries in Python, you can read the official documentation at https://docs.python.org/3/library/stdtypes.html#dict.

Give a Clap and Follow me for more helpful content! Share if you like :)

Please visit my other articles as well.

Happy Pythoning!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Yashuv Baskota
Yashuv Baskota

Written by Yashuv Baskota

Python | Data Science and Machine Learning Practitioner | Learner | "The only way to do great work is to love what you do." - Steve Jobs

No responses yet

Write a response