> ## Documentation Index
> Fetch the complete documentation index at: https://rajanand.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Lists

## 1. **What is a List?**

* A **list** is a collection of items that are ordered, mutable (changeable), and allow duplicate elements.
* Lists are one of the most versatile data structures in Python.
* Lists are defined using square brackets `[]`.

**Example**:

```python theme={"system"}
fruits = ["apple", "banana", "cherry"]
```

## 2. **List Operations**

### **1. Accessing Elements**

* Use indexing to access individual elements in a list.
* Python uses zero-based indexing (the first element has index `0`).

**Example**:

```python theme={"system"}
fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Output: apple
print(fruits[-1])  # Output: cherry (negative indexing starts from the end)
```

### **2. Slicing**

* Extract a sublist using a range of indices.
* Syntax: `list[start:stop:step]`

**Example**:

```python theme={"system"}
numbers = [1, 2, 3, 4, 5]
print(numbers[1:4])  # Output: [2, 3, 4] (from index 1 to 3)
print(numbers[::2])  # Output: [1, 3, 5] (every second element)
```

### **3. Modifying Elements**

* Lists are mutable, so you can change their elements.

**Example**:

```python theme={"system"}
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']
```

### **4. Adding Elements**

* Use `append()` to add an element to the end of the list.
* Use `insert()` to add an element at a specific position.
* Use `extend()` to add multiple elements from another list.

**Examples**:

```python theme={"system"}
fruits = ["apple", "banana"]
fruits.append("cherry")  # Add to the end
print(fruits)  # Output: ['apple', 'banana', 'cherry']

fruits.insert(1, "blueberry")  # Insert at index 1
print(fruits)  # Output: ['apple', 'blueberry', 'banana', 'cherry']

fruits.extend(["mango", "orange"])  # Add multiple elements
print(fruits)  # Output: ['apple', 'blueberry', 'banana', 'cherry', 'mango', 'orange']
```

### **5. Removing Elements**

* Use `remove()` to remove the first occurrence of a value.
* Use `pop()` to remove an element at a specific index (or the last element if no index is provided).
* Use `del` to delete an element or a slice of elements.
* Use `clear()` to remove all elements from the list.

**Examples**:

```python theme={"system"}
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")  # Remove the first "banana"
print(fruits)  # Output: ['apple', 'cherry', 'banana']

fruits.pop(1)  # Remove element at index 1
print(fruits)  # Output: ['apple', 'banana']

del fruits[0]  # Delete element at index 0
print(fruits)  # Output: ['banana']

fruits.clear()  # Remove all elements
print(fruits)  # Output: []
```

### **6. Finding Elements**

* Use `index()` to find the index of the first occurrence of a value.
* Use `in` to check if an element exists in the list.

**Examples**:

```python theme={"system"}
fruits = ["apple", "banana", "cherry"]
print(fruits.index("banana"))  # Output: 1
print("cherry" in fruits)  # Output: True
```

### **7. Sorting**

* Use `sort()` to sort the list in place (modifies the original list).
* Use `sorted()` to return a new sorted list (does not modify the original list).

**Examples**:

```python theme={"system"}
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort()
print(numbers)  # Output: [1, 1, 3, 4, 5, 9]

sorted_numbers = sorted(numbers, reverse=True)  # Sort in descending order
print(sorted_numbers)  # Output: [9, 5, 4, 3, 1, 1]
```

### **8. Reversing**

* Use `reverse()` to reverse the list in place.

**Example**:

```python theme={"system"}
fruits = ["apple", "banana", "cherry"]
fruits.reverse()
print(fruits)  # Output: ['cherry', 'banana', 'apple']
```

### **9. Length**

* Use `len()` to find the number of elements in a list.

**Example**:

```python theme={"system"}
fruits = ["apple", "banana", "cherry"]
print(len(fruits))  # Output: 3
```

## 3. **List Comprehensions**

* A concise way to create lists using a single line of code.
* Syntax: `[expression for item in iterable if condition]`

**Examples**:

```python theme={"system"}
squares = [x ** 2 for x in range(5)]
print(squares)  # Output: [0, 1, 4, 9, 16]

even_numbers = [x for x in range(10) if x % 2 == 0]
print(even_numbers)  # Output: [0, 2, 4, 6, 8]
```

## 4. **Nested Lists**

* A list can contain other lists, creating a multi-dimensional structure.

**Example**:

```python theme={"system"}
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2])  # Output: 6 (access element in the second row, third column)
```

## 5. **Additional Examples**

* **Creating a List**:
  ```python theme={"system"}
  names = ["Raj", "Ram", "Anand", "Bala"]
  print(names)  # Output: ['Raj', 'Ram', 'Anand', 'Bala']
  ```

* **Adding Elements**:
  ```python theme={"system"}
  names.append("Karthik")
  print(names)  # Output: ['Raj', 'Ram', 'Anand', 'Bala', 'Karthik']
  ```

* **Removing Elements**:
  ```python theme={"system"}
  names.remove("Ram")
  print(names)  # Output: ['Raj', 'Anand', 'Bala', 'Karthik']
  ```

* **Sorting**:
  ```python theme={"system"}
  names.sort()
  print(names)  # Output: ['Anand', 'Bala', 'Karthik', 'Raj']
  ```

* **List Comprehension**:
  ```python theme={"system"}
  name_lengths = [len(name) for name in names]
  print(name_lengths)  # Output: [5, 4, 7, 3]
  ```

## 6. **Best Practices**

* Use list comprehensions for concise and readable code.
* Avoid modifying a list while iterating over it.
* Use `append()` instead of `+` for adding elements to a list (more efficient).
