# Как убрать из списка дубликат элемента?

### **1. Через множество (`set`)**

* Преобразуем список в `set`, а потом обратно в `list`.
* Минус: **теряется порядок элементов**.

```python
lst = [1, 2, 2, 3, 4, 3]
unique_lst = list(set(lst))
print(unique_lst)  # Порядок может быть [1, 2, 3, 4] или другой
```

***

### **2. Через `dict.fromkeys()`**

* Сохраняет **порядок элементов** (Python 3.7+).

```python
lst = [1, 2, 2, 3, 4, 3]
unique_lst = list(dict.fromkeys(lst))
print(unique_lst)  # [1, 2, 3, 4]
```

***

### **3. Через генератор с проверкой**

* Можно вручную проверять, что элемент ещё не встречался.
* Сохраняется порядок.

```python
lst = [1, 2, 2, 3, 4, 3]
unique_lst = []
for item in lst:
    if item not in unique_lst:
        unique_lst.append(item)
print(unique_lst)  # [1, 2, 3, 4]
```

***

### **4. Через `collections.OrderedDict`** (для старых версий Python)

```python
from collections import OrderedDict

lst = [1, 2, 2, 3, 4, 3]
unique_lst = list(OrderedDict.fromkeys(lst))
print(unique_lst)  # [1, 2, 3, 4]
```

***

#### **5. Вывод**

| Способ          | Сохраняет порядок | Быстрота  | Простой           |
| --------------- | ----------------- | --------- | ----------------- |
| `set`           | ❌                 | Быстро    | Да                |
| `dict.fromkeys` | ✅                 | Быстро    | Да                |
| генератор       | ✅                 | Медленнее | Да                |
| `OrderedDict`   | ✅                 | Быстро    | Да, но устаревший |

***

💡 В **автотестах** часто используют `dict.fromkeys()` или set, чтобы убрать дубликаты из **списка элементов UI, API ответов или тестовых данных**.


---

# 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://kaze.gitbook.io/qa-theory/osnovy-programmirovaniya-na-python/kak-ubrat-iz-spiska-dublikat-elementa.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.
