Python Dictionary
By: Daniel Liu
Tags: Data Structures, Python, Dictionary, Computer Science, Python Dictionaries

Overview

A dictionary in python is unordered, changeable and indexed. It is unordered because it does not keep track of the order of data inserted. It is changeable because you can change the values of keys. It is indexed because it retrieves data based on the key meaning each key only has one value.

New Dictionary

To create a dictionary you can use curly braces or the dict() constructor.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

} otherDictionary = dict(name="Bob", age=21, email="bob@emai.com")

It is possible to have empty string and None value as keys and values. It is not possible to have duplicate keys.

Read

To get values from the dictionary you need to provide the key inside square brackets or use the get() method

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

} print(dictionary["name"])

>> Bob

print(dictionary.get("age"))

>> 21

Update

To change the value of the key you need to reference the key similar to how you would reference the index of a list.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

}

dictionary["name"] = "Mike"

print(dictionary["name"])

>> Mike

Iteration

You can iterate through the keys, values or both using a for loop.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

} for key in dictionary:

print(key)

>> name

>> age

>> email for key in dictionary:

print(dictionary[key])

>> Bob

>> 21

>> bob@email.com for key, value in dictionary.items:

print(key, value)

>> name Bob

>> age 21

>> email bob@email.com

Length

To check the length of the dictionary you can use the len() method

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

}

print(len(dictionary))

>> 3

Create

To create a new key and value in an existing dictionary you can reference the new key and give it a value.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

}

dictionary["job"] = "Teacher"

Delete

To delete a key and value you can use the pop() method by providing it the key.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

}

dictionary.pop("email")

print(dictionary)

>> {'age': 21, 'age': 21}

In python version 3.7 and above you can use the popitem() method to delete the most recent key and value.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

}

dictionary["job"] = "teacher"

dictionary.popitem()

print(dictionary)

>> {'age': 21, 'name': 'Bob', 'email': 'bob@email.com'}

To clear the entire dictionary but keep the data structure you can use the clear() method.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

}

dictionary.clear()

print(dictionary)

>> {}

Copy

To create a copy of a dictionary you can use the copy method or pass the copy method to the dict() constructor.

dictionary = {

"name": "Bob",

"age": 21,

"email": "bob@email.com"

}

dict1 = dictionary.copy()

dict2 = dict(dictionary)

Nested Dictionary

It is possible to have a dictionary within a dictionary by having a key and another dictionary as a value.

students = {

"One" : {

"name": "Bob",

"age": "10"

},

"Two" : {

"name": "Jack",

"age": "9"

},

}

Conclusion

Pythons dictionary is similar to Javas Hashmap where both supports key value pairs, does not have duplicate keys, are unordered, changeable and indexed. You would use dictionaries when you want to associate certain values with keys. Because dictionaries are hashed it makes storing and retrieving values faster than an ordered list.