Dictionaries

Dictionaries

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.
Basically an actual dictionary but in code.

Creating a Dictionary

Here's how you create a dictionary:

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

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

Accesssing 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:

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

    print(queens["state"])
	
	
    New York
	

Adding Items

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

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

    queens["organization"] = "United Nations"

    print(queens)
	
	
    {'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:

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

    queens["state"] = "New Jersey"

    print(queens["state"])
	
	
    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:

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

    for key in queens:
        value = queens[key]
        print("Key: " + key + " Value: " + value)
	
	
    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:

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

    for key, value in queens.items():
        print("Key: " + key + " Value: " + value)
	
	
    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:

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

    print("city" in queens)
    print("town" in queens)
	
	
    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:

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

    queens.pop("state")

    print(queens)
	
	
    {'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:

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

    print(len(queens))
	
	
    3