Dictionaries are collections that are unordered, changeable, and are indexed. They are Python's implementation of a key-value pair collection. They are similar to lists but instead of the index being a number, you can use the key to access the element instead.

Basically an actual dictionary but in code.

Creating a Dictionary

Here's how you create a dictionary:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } print(queens)
BASH
{'city': 'New York City', 'state': 'New York', 'country': 'United States'}

Accessing Items

The syntax for accessing items in a dictionary is similar to the syntax for accessing items in a list except that you use the key instead of the index:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } print(queens["state"])
BASH
New York

Adding Items

You can add items to a dictionary by setting a new key-value pair, like this:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } queens["organization"] = "United Nations" print(queens)
BASH
{'city': 'New York City', 'state': 'New York', 'country': 'United States', 'organization': 'United Nations'}

Changing Items

Dictionaries allow you to change the value of an item if you refer to it directly:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } queens["state"] = "New Jersey" print(queens["state"])
BASH
New Jersey

Looping through a Dictionary

You can iterate through the keys of a dictionary, and then use that key to get the value. Here's how:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } for key in queens: value = queens[key] print("Key: " + key + " Value: " + value)
BASH
Key: city Value: New York City Key: state Value: New York Key: country Value: United States

Alternatively, you can use the items() function to accomplish the same thing like this:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } for key, value in queens.items(): print("Key: " + key + " Value: " + value)
BASH
Key: city Value: New York City Key: state Value: New York Key: country Value: United States

Checking if a Key Exists

You can check if a key exists inside a dictionary like this:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } print("city" in queens) print("town" in queens)
BASH
True False

Removing Items

You can remove items from a dictionary by using the pop() function and providing the key. Here's how that looks:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } queens.pop("state") print(queens)
BASH
{'city': 'New York City', 'country': 'United States'}

Dictionary Length

You can get the number of items that exist in your dictionary by using the len() function:

PYTHON
queens = { "city": "New York City", "state": "New York", "country": "United States" } print(len(queens))
BASH
3
Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.