Home Tutorials Python Tutorial Dictionaries
Dictionaries

Dictionaries


Definition: A dictionary is an ordered (as of Python 3.7) and changeable collection of data stored in key-value pairs. Instead of indexing items by numbers, dictionaries look up values using unique keys.

Why: Dictionaries are a core built-in data structure because they allow you to map associations between related pieces of information (like a word and its meaning, or a user and their account profile).

Syntax

dictionary_name = {
    "key1": "value1",
    "key2": "value2"
}

Example

student = {
    "name": "Anu",
    "age": 19,
    "marks": 88
}

print(student)
print(student["name"])
print(student["marks"])

Explanation

  • Curly Brackets & Colons: Dictionaries are defined with curly brackets { }, and each key is separated from its value by a colon :.
  • Keys: In this example, "name", "age", and "marks" are the keys. They act as labels to retrieve data.
  • Values: "Anu", 19, and 88 are the values associated with those keys.
  • Accessing Data: Passing a key in square brackets (e.g., student["name"]) retrieves its corresponding value.

Key Notes

  • Keys must be unique; you cannot have two identical keys in a single dictionary. If you assign a value to an existing key, the old value gets overwritten.
  • Keys are case-sensitive (e.g., "Age" and "age" are treated as two different keys).
  • Values can be of any data type, including strings, integers, booleans, or even lists.
Example

🏋️ Test Yourself With Exercises

Take our quiz on Dictionaries to test your knowledge.

Exercise »