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
Getting Started with Electron
Git Tutorial: Learn how to use Version Control
How to deploy a PHP app using Docker
How to deploy a Deno app using Docker
How to deploy an Express app using Docker
Using Puppeteer and Jest for End-to-End Testing
How to Scrape the Web using Node.js and Puppeteer
Getting Started with Moment.js
Using Push.js to Display Web Browser Notifications
Getting Started with React
Setting Up Stylus CSS Preprocessor
Using Axios to Pull Data from a REST API
