# Lists

Lists are ordered, mutable collections. They can hold mixed types and be resized as needed.

## Creating Lists

```python
numbers = [1, 2, 3]
mixed = ["Apple", 12, 45.7, False]
nested = ["Apple", ["Banana", "Cherry"], "Durian"]
```

## Accessing Items

```python
fruits = ["Apple", "Banana", "Cherry"]

print(fruits[0])   # Apple
print(fruits[-1])  # Cherry
```

## Slicing

```python
letters = ["P", "y", "t", "h", "o", "n"]
print(letters[2:5])  # ['t', 'h', 'o']
print(letters[:3])   # ['P', 'y', 't']
print(letters[3:])   # ['h', 'o', 'n']
```

## Updating Items

```python
odd = [2, 4, 6, 8]
odd[0] = 1
odd[1:4] = [3, 5, 7]
print(odd)  # [1, 3, 5, 7]
```

## Adding Items

```python
fruits = ["Apple", "Banana"]
fruits.append("Cherry")
fruits.extend(["Durian", "Mango"])
fruits.insert(1, "Grape")
```

## Removing Items

```python
fruits = ["Apple", "Banana", "Cherry", "Durian"]
fruits.remove("Banana")
last = fruits.pop()
del fruits[0]
fruits.clear()
```

## Common Methods

| Method      | Purpose                        |
| ----------- | ------------------------------ |
| `append()`  | add one item to end            |
| `extend()`  | add many items                 |
| `insert()`  | insert at index                |
| `remove()`  | remove by value                |
| `pop()`     | remove by index (default last) |
| `clear()`   | remove all items               |
| `index()`   | find first index               |
| `count()`   | count occurrences              |
| `sort()`    | sort list in place             |
| `reverse()` | reverse in place               |
| `copy()`    | shallow copy                   |

## List Comprehensions

```python
powers = [2 ** x for x in range(5)]
odds = [x for x in range(10) if x % 2 == 1]
```

## Iteration

```python
for fruit in ["Apple", "Banana"]:
    print(fruit)
```

[Next](/cit-python-cohort-three/week2/data-types/python_tuples.md) | [Previous](/cit-python-cohort-three/week2/data-types/strings.md)


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
