StackTips

Python: Working with Dictionaries

stacktips avtar

Written by

Editorial,  6 min read,  2.01K views, updated on Sept. 17, 2023

Unlike all other collection types, dictionaries strictly contain key-value pairs.

Dictionaries are collections that are unordered, changeable, and are indexed.

They are similar to lists but instead of the index being a number, you can instead use the key to access the element.

Creating a Dictionary

Here's how you create a dictionary:

queens = {
    "city": "New York City",
    "state": "New York",
    "country": "United States"
}

print(queens)   

Output:

{'city': 'New York City', 'state': 'New York', 'country': 'United States'}

Accessing Items

Items in a dictionary can be accessed using the key.

queens = {
    "city": "New York City",
    "state": "New York",
    "country": "United States"
}

print(queens["city"])

Output:

New York

Adding Items to Python Dictionary

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

company = {
    "city": "New York City",
    "state": "New York",
    "country": "United States"
}
company["name"] = "ABC Inc"
print(company)

Output:

{'city': 'New York City', 'state': 'New York', 'country': 'United States', 'name': 'ABC Inc'}

Update Items in Python Dictionary

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

company = {
    "city": "New York City",
    "state": "New York",
    "country": "United States"
}
company["city"] = "London"
company["country"] = "GB"
print(company)
{'city': 'London', 'state': 'New York', 'country': 'GB'}

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:

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

Output:

Key: city Value: New York City
Key: state Value: New York
Key: country Value: United Statess

Checking if a Key Exists

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

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

Output:

True

Removing Items

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

queens = {
    "city": "New York City",
    "state": "New York",
    "country": "United States"
}

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

Dictionary Length

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

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

Output:

{'city': 'New York City', 'state': 'New York', 'country': 'United States'}
3

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE

Continue reading..