7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

Python Tutorial

Python Dictionary

Ready to start your career in Data? Find out more about BrainStation's Python Course

Python dictionaries are another powerful data type for representing information in Python. Dictionaries are an unordered collection of key-value pairs. There are many different ways of creating a Python dictionary however the easiest way is to use curly brackets {}. A dictionary is of the form

My_dict = {key: value, anotherKey: anotherValue}

Some of the important things to remember about dictionaries are:

  • Each key in the dictionary has to be unique
  • Unlike list, dictionaries are indexed by keys
  • A specific value in the dictionary can be retrieved by using it’s key.
  • Multiple key value pairs are separated by comma ,
  • Key and value are separated by a colon :

Creating a Python dictionary

Let’s create a python dictionary using

  • Curly brackets
  • using dict()

Accessing a Value in Dictionary

Values in the dictionary can be accessed by using it’s relevant keys inside of a square bracket []. Let’s try to access the name and email of students adam and john we just created.

Updating a Value in Dictionary

Values in the dictionary can be updated by using its key in a square bracket and assigning a new value to it using an equal sign =. Let’s update the phone number for both students adam and john

Deleting a Value in Dictionary

Values in the dictionary can be deleted by using the pop method and specify the key of the value to be deleted as pop(key). Deleting a value also removes the entire key-value pair from the dictionary. Let’s try to delete the address for student john.

Deleting Entire Dictionary

An entire dictionary can be deleted using the del keyword. This deletes the entire dictionary and not just the key-value pairs in it. Let’s delete our dictionary structure for student adam. As you can see below, accessing the dictionary after deleting it throws an error.

Set Password

You already have an account with BrainStation, but you still need to set up a password.