Unlock the Power of Python Dictionaries: The Ultimate Guide for Beginners

StarTek
5 min readDec 9, 2022

--

Hey there, Python beginners! Are you ready to learn about the dictionary, one of the most useful data types in the Python language? Dictionaries, also known as "dicts," are a way to store data in a flexible, organized way. They're like a list, but instead of using numbers to access the data, you use a "key" - which can be a word, a number, or even a combination of both.

Here are the key points we'll cover in this article:

  • What dictionaries are and why they're useful
  • How to create and access dictionaries in Python
  • Some common ways to use dictionaries in your code

Remember, this article is part of a Python beginners series, so be sure to check out the rest of the articles here.

Now, let's dive into dictionaries!

Photo by Danylo Suprun on Unsplash

What are dictionaries, and why are they useful?

At their core, dictionaries are a way to store data in a flexible, organized way. Let's say you have a list of people's names, and you want to store some information about each person - like their age, their favorite color, and their favorite hobby. You could create a list like this:

people = ["Alice", "Bob", "Charlie", "David"]

But how would you store the information about each person's age, favorite color, and hobby? You could create separate lists for each piece of information, but that would get messy quickly. And what if you want to access the information for a specific person - how would you do that?

This is where dictionaries come in. With a dictionary, you can store all of the information for each person in a single "item" - and you can access that information using a "key" instead of a number. So, instead of creating separate lists, you could create a dictionary like this:

people = {
"Alice": {"age": 25, "favorite_color": "blue", "favorite_hobby": "painting"},
"Bob": {"age": 30, "favorite_color": "green", "favorite_hobby": "hiking"},
"Charlie": {"age": 35, "favorite_color": "red", "favorite_hobby": "reading"},
"David": {"age": 40, "favorite_color": "orange", "favorite_hobby": "dancing"},
}

Now, each person has their own "item" in the dictionary, and you can access that item using the person's name as a key. So, if you want to find out Bob's favorite hobby, you can do this:

# This would set the variable "bob_hobby" to the string "hiking"
bob_hobby = people["Bob"]["favorite_hobby"]

See how much easier that is than trying to use separate lists? Dictionaries are a great way to store and organize data in Python.

How to create and access dictionaries in Python

Now that you know why dictionaries are useful, let's talk about how to create and access them in Python. To create a dictionary, you use curly braces ({}) and colons (:) to separate the keys and values. Here's an example of a simple dictionary:

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

This dictionary has three items, each with a different key (”key1", ”key2", and ”key3") and a corresponding value (”value1", ”value2", and ”value3"). To access a value in a dictionary, you use the dictionary’s name followed by the key in square brackets ([]), like this:

# This would set the variable "value1" to the string "value1"
value1 = my_dict["key1"]

If you try to access a key that doesn’t exist in the dictionary, you’ll get an error — so it’s important to make sure the key you’re using is correct.

Some common ways to use dictionaries in your code

Now that you know the basics of dictionaries, let’s talk about some common ways you can use them in your code. One of the most common uses for dictionaries is to store information about a particular object or concept. For example, you could use a dictionary to store information about a person, like we did in the earlier example.

Another common use for dictionaries is to store data that you want to look up quickly, like a list of words and their definitions. For example, you could create a dictionary like this:

dictionary = {
"apple": "a round fruit with red or green skin",
"orange": "a round fruit with orange skin",
"banana": "a long, curved fruit with yellow skin",
}

Now, if you want to look up the definition of the word “apple,” you can do it like this:

# This would set the variable "apple_definition" to the string "a round fruit with red or green skin"
apple_definition = dictionary["apple"]

Dictionaries are also useful for counting things. For example, let’s say you have a list of words, and you want to count how many times each word appears in the list. You could do it like this:

words = ["apple", "orange", "banana", "apple", "banana"]

word_counts = {}

for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1

# Now the dictionary "word_counts" will look like this:
# {
# "apple": 2,
# "orange": 1,
# "banana": 2,
# }

Here, we use a for-loop to iterate through the list of words and check if each word exists in the dictionary. If it does, we add 1 to the word’s “count” in the dictionary. If it doesn’t, we add the word to the dictionary with a count of 1. This is a quick and easy way to count the items in a list using a dictionary.

Those are just a few examples of how you can use dictionaries in your code. There are many other ways to use them, and I encourage you to experiment and see what you can come up with.

In conclusion, dictionaries are a powerful and flexible data type in Python. They allow you to store and organize data in a way that’s easy to access and modify. Whether you’re storing information about people, looking up definitions, or counting items in a list, dictionaries are a great tool

I hope you’ve enjoyed reading this article and have learned something new. If you have, please consider subscribing to stay up-to-date with my latest posts. Simply subscribing helps me out a lot and will allow me continue creating valuable content.

Thank you for your support!

--

--

StarTek
StarTek

Written by StarTek

I hope you've learned something new!

No responses yet