Table of Contents
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:
PYTHONqueens = {
"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:
PYTHONqueens = {
"city": "New York City",
"state": "New York",
"country": "United States"
}
print(queens["state"])
BASHNew York
Adding Items
You can add items to a dictionary by setting a new key-value pair, like this:
PYTHONqueens = {
"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:
PYTHONqueens = {
"city": "New York City",
"state": "New York",
"country": "United States"
}
queens["state"] = "New Jersey"
print(queens["state"])
BASHNew 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:
PYTHONqueens = {
"city": "New York City",
"state": "New York",
"country": "United States"
}
for key in queens:
value = queens[key]
print("Key: " + key + " Value: " + value)
BASHKey: 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:
PYTHONqueens = {
"city": "New York City",
"state": "New York",
"country": "United States"
}
for key, value in queens.items():
print("Key: " + key + " Value: " + value)
BASHKey: 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:
PYTHONqueens = {
"city": "New York City",
"state": "New York",
"country": "United States"
}
print("city" in queens)
print("town" in queens)
BASHTrue
False
Removing Items
You can remove items from a dictionary by using the pop() function and providing the key. Here's how that looks:
PYTHONqueens = {
"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:
PYTHONqueens = {
"city": "New York City",
"state": "New York",
"country": "United States"
}
print(len(queens))
BASH3
How to Install Node on Windows, macOS and Linux
Getting Started with Svelte
Getting Started with Express
How to Serve Static Files with Nginx and Docker
Best Visual Studio Code Extensions for 2022
How to deploy a PHP app using Docker
How to deploy a MySQL Server using Docker
How to deploy an Express app using Docker
Build a Real-Time Chat App with Node, Express, and Socket.io
Getting Started with React
Getting Started with Vuex: Managing State in Vue
How To Create a Modal Popup Box with CSS and JavaScript
