> For the complete documentation index, see [llms.txt](https://docs.arkannis.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.arkannis.net/programming/courses/python-pcap-31-03-course/lists-tuples-and-dictionaries/dictionaries.md).

# Dictionaries

## Dictionaries in Python

```python
# Define dict:
dict = {
	'k1': 'some data',
	7: ['can', 'be', 'a', 'list']
}

# Dictionaries are not position oriented, they are key oriented
# To get the data:
dict['k1'] # prints 'some data'

print(dict[7]) # prints ['can', 'be', 'a', 'list']
```

**Dictionaries are mutable and can be changed**

```python
dict[7] = 'new value'
```

**You cannot sort dictionaries, you should use keys to get the data**

```python
people_weight_dict = {
	'john': 123,
	'mike': 170,
	'robert': 167
}

# To change john's weight you would do
people_weight_dict['john'] = 134
```

**You can pop items from dictionaries**

```python
people_weight_dict.pop('mike')
```

**Syntax:**

```python
dict.pop('key')
```

#### Clear method

```python
people_weight_dict.clear()
# It will still be a dictionary but it will be an empty one
```

### Adding items to dictionary:

```python
people_weight_dict = {
	'john': 123,
	'mike': 170,
	'robert': 167
}
# Adding the data
people_weight_dict['new_key'] = 'new data to be added'

# At this point the dict has been permanently modified and adjusted with a new entry
```

**Accessing item from a list in the dict**

```python
people_weight_dict = {
	'john': 123,
	'mike': 170,
	'robert': 167,
	'items': ['apple', 'banana', 'orange']
}

print(people_weight_dict['items'][2]) # prints orange
```

**Accessing items from a dictionary in a list in a dict**

```python
people_weight_dict = {
	'john': 123,
	'mike': 170,
	'robert': 167,
	'items': ['apple', 'banana', {'k1': 'some value'}]
}
print(people_weight_dict['items'][2]['k1']) # prints 'some value'
```

**Remove items for dictionary:**

```python
people_weight_dict = {
	'john': 123,
	'mike': 170,
	'robert': 167,
	'items': ['apple', 'banana', {'k1': 'some value'}]
}

people_weight_dict.pop['items']
print(people_weight_dict) 
# It will print:
people_weight_dict = {
	'john': 123,
	'mike': 170,
	'robert': 167
}
```


---

# 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://docs.arkannis.net/programming/courses/python-pcap-31-03-course/lists-tuples-and-dictionaries/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.
