# 索引错误：列表索引超出范围

- **ID:** `python/indexerror-list-index-out-of-range`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

访问列表中不存在的索引，例如长度为 3 的列表使用索引 5。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |

## 解决方案

1. **Check list length before access** (95% 成功率)
   ```
   if len(my_list) > index: value = my_list[index]
   ```
2. **Use .get() equivalent for lists** (90% 成功率)
   ```
   value = my_list[index] if index < len(my_list) else None
   ```

## 无效尝试

- **Using try-except to ignore IndexError** — Ignores the problem; test may pass incorrectly. (70% 失败率)
- **Using negative indexing blindly** — Negative indexing may access unintended elements if list length changes. (50% 失败率)
