Understanding Dictionaries in Python

·

3 min read

Understanding Dictionaries in Python

Dictionaries are a really essential concept for coding in Python. They are often used in places where you need to store a massive collection of data which is organized in a particular order (post Python 3.6).

Dictionaries commonly follow a key-value pair format. This is similar to hash maps in common data structures.

1. Declaration

Dictionaries open and close with keys and values contained inside curly brackets. They can be declared in the following way:

sampleDict = {
    "name": "Sam",
    "age": "19",
}

This gives you a dictionary called sampleDict which stores, for example, the general information of a person.

2. Accessing Data

You can reference and access data stored in multiple ways. One way to do so would be the following way:

print(sampleDict["name"])

Displays the value of the key name:

Sam

You can also access data by assigning sampleDict["name"] to a variable and printing the variable such as follows:

x = sampleDict["name"]
print(x)

This would display the value of the 'name'. Similar to the code above.

Sam

Another way to access data is to use the get() method:

x = sampleDict.get("name")
print(x)

If you want just a list of all keys in the dictionary, the key() method returns a list of all the keys.

print(sampleDict.keys())

The output is as follows:

dict_keys(["name", "age"])

Similarly, if you want a list of all values in a dictionary, values() method can be used:

print(sampleDict.values())

Gives you the output:

dict_values(["Sam", "19"])

3. Adding and Deleting Data

There will be occasions where you will want to modify the contents of a dictionary. Sometimes, you will need to add new information and sometimes, you'll want to delete old information.

To add new data, you declare a new key and assign a value to it:

sampleDict["school"] = "XYZ"

The modified dictionary looks like this:

sampleDict = {
    "name": "Sam",
    "age": "19",
    "school": "XYZ"
}

Another way to achieve the same result would be to use the update() method:

sampleDict.update({"school": "XYZ"})

The update() method could also be used to update the dictionary keys with new arguments.

To remove a specific key, you can use the pop() method:

sampleDict.pop("school")
print(sampleDict)

The dictionary will then look something like this:

sampleDict = {
    "name": "Sam",
    "age": "19",
}

Another way to delete the school key is to use the del keyword.

del sampleDict["school"]

4. Modifying Data

You can modify a key by referencing it along with the new value:

sampleDict = {
    "name": "Sam",
    "age": "19",
}
sampleDict["school"] = "XYZ"
print(sampleDict)

The dictionary will have the new key:

sampleDict = {
    "name": "Sam",
    "age": "19",
    "school": "XYZ"
}

Another way to update an item is to use the update() method. The code is the same as mentioned above.

5. Common Methods

clear() is used to clear the all the elements from a dictionary.

copy() is used to create another copy of the dictionary.

fromkeys(keys, values) takes two variables with one list of keys and one list of values/single values and returns a dictionary.

A lot of these concepts are like bread and butter while using dictionaries. You will find yourself using them quite a bit during your coding sessions so mastering them is a good idea.