> For the complete documentation index, see [llms.txt](https://kallyasmedia.gitbook.io/cit-python-cohort-three/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kallyasmedia.gitbook.io/cit-python-cohort-three/week2/data-types/dictionaries.md).

# Dictionaries

Dictionaries store key/value pairs. Keys must be unique and immutable.

## Creating Dictionaries

```python
empty = {}
person = {"name": "Jack", "age": 26}
mixed = {1: "apple", "colors": ["red", "green"]}

from_pairs = dict([(1, "a"), (2, "b")])
```

## Accessing Values

```python
print(person["name"])      # Jack
print(person.get("age"))   # 26
print(person.get("city"))  # None
```

## Adding and Updating

```python
person["age"] = 27
person["city"] = "Downtown"
```

## Removing

```python
scores = {1: 1, 2: 4, 3: 9}
scores.pop(2)     # removes key 2
scores.popitem()  # removes last inserted item (Python 3.7+)
del scores[1]
scores.clear()
```

## Common Methods

```python
person = {"name": "Jack", "age": 26}

print(person.keys())
print(person.values())
print(person.items())
```

## Dictionary Comprehension

```python
squares = {x: x * x for x in range(6)}
odd_squares = {x: x * x for x in range(11) if x % 2 == 1}
```

## Membership and Iteration

```python
print("name" in person)  # True

for key, value in person.items():
    print(key, value)
```

[Previous](/cit-python-cohort-three/week2/data-types/sets.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://kallyasmedia.gitbook.io/cit-python-cohort-three/week2/data-types/dictionaries.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
