Dictionary

A dictionary is a data structure in Python that stores a collection of key-value pairs. Each key-value pair in a dictionary is separated by a colon ( : ), and each pair is separated by a comma ( , ). Dictionaries are useful for storing data in a way that allows for quick and easy lookup of values by their corresponding keys.

Creating a Dictionary:

To create a dictionary in Python, we use a set of curly braces {} and fill it with a set of key-value pairs. Here's an example:


            # create a dictionary of names and ages
            ages = {"Alice": 25, "Bob": 30, "Charlie": 35}

            # print the dictionary
            print(ages)
        

This will output:


            {'Alice': 25, 'Bob': 30, 'Charlie': 35}
        

Accessing values in a dictionary

To access a value in a dictionary, we can use the key associated with that value. Here's an example:


            # get the age of Alice
            print(ages["Alice"]) # 25

            # get the age of Bob
            print(ages["Bob"]) # 30
        

Adding and updating values in a dictionary

To add a new key-value pair to a dictionary, we can simply assign a value to a new key:


            # add a new key-value pair to the dictionary
            ages["David"] = 40

            # print the updated dictionary
            print(ages)
        

This will output:


            {'Alice': 25, 'Bob': 30, 'Charlie': 35, 'David': 40}
        

To update the value associated with a key, we can simply re-assign a new value to that key:


            # update the age of Alice
            ages["Alice"] = 26

            # print the updated dictionary
            print(ages)
        

This will output:


            {'Alice': 26, 'Bob': 30, 'Charlie': 35, 'David': 40}
        

Removing values from a dictionary

To remove a key-value pair from a dictionary, we can use the del keyword:


            # remove the age of Bob from the dictionary
            del ages["Bob"]

            # print the updated dictionary
            print(ages)
        

This will output:


            {'Alice': 26, 'Charlie': 35, 'David': 40}
        

Looping through a dictionary

We can loop through a dictionary using a for loop. By default, the loop will iterate over the keys in the dictionary, but we can also iterate over the values or key-value pairs by using the values() and items() methods, respectively. Here's an example:


            # loop through the keys in the dictionary
            for name in ages:
                print(name)

            # loop through the values in the dictionary
            for age in ages.values():
                print(age)

            # loop through the key-value pairs in the dictionary
            for name, age in ages.items():
                print(name, age)
        

This will output:


            Alice
            Charlie
            David

            26
            35
            40

            Alice 26
            Charlie 35
            David 40
        

Conclusion

You had just learned about dictionaries in Python. Dictionaries are a data structure that allows us to store key-value pairs for quick and easy lookup of values by their corresponding keys. We learned how to create, access, add, update, and remove values from a dictionary, as well as how to loop through a dictionary using a for loop. These concepts are important for working with dictionaries in Python and for solving problems