How to remove duplicate elements in Python3 list

Remove duplicate elements from the list

The reason I decided to write this list was that when I created a list with no duplicate elements, I used remove () to remove the elements. I misunderstood that using remove () would remove all the specified values.

l = [1, 2, 3, 4, 1]
l.remove(1)
print(l)
#[2, 3, 4, 1]

In this way, you can delete the specified value that appears first. However, with this code, only one can be deleted, so the following behavior will occur.

l = [1, 2, 3, 4, 1, 1]
l.remove(1)
print(l)
#[2, 3, 4, 1, 1]

Therefore, in order to create a unique list, the code should look like this:

l = [3, 4, 3, 2, 5, 4, 3]
l_u = []
for i in l:
    if i not in l_u:
        l_u.append(i)
#[3, 4, 2, 5]

This allows you to create a unique list of elements.

Referenced site https://www.lifewithpython.com/2013/11/python-remove-duplicates-from-lists.html https://note.nkmk.me/python-list-clear-pop-remove-del/

Postscript

I will add it because I received a comment. It seems that the following code can be executed faster than the code described above. You can also save the order.

l = [3, 4, 3, 2, 5, 4, 3]
print(sorted(set(l), key=l.index))
#[3, 4, 2, 5]

Recommended Posts

How to remove duplicate elements in Python3 list
[Python] How to remove duplicate values from the list
How to clear tuples in a list (Python)
Getting list elements in Python
[Python] How to sort dict in list and instance in list
[Python] How to output the list values in order
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Delete multiple elements in python list
How to swap elements in an array in Python, and how to reverse an array.
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Data science companion in python, how to specify elements in pandas
How to check in Python if one of the elements of a list is in another list
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Python] How to convert a 2D list to a 1D list
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
Summary of how to use Python list
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to remove duplicates from a Python list while preserving order.
Sorted list in Python
How to use the C library in Python
How to receive command line arguments in Python
Sorted list in Python
[REAPER] How to play with Reascript in Python
How to install Python
How to generate permutations in Python and C ++
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
Filter List in Python
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
[Python] How to put any number of standard inputs in a list
Summary of how to use MNIST in Python
How to specify TLS version in python requests
Duplicate combinations in Python
List find in Python
[Algorithm x Python] How to use the list
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
How to notify a Discord channel in Python
How to get the files in the [Python] folder