How to remove duplicates from a Python list while preserving order.

** This method can be used from Python 3.7 or later. ** **

From Python 3.7, it seems that ** dictionary order is maintained **. fromkeys() A dictionary can be generated as follows.

Input print (dict.fromkeys (["key 1 "," key 2 "," key 3 "]," value ")) Output {"Key 1 ":" Value "," Key 2 ":" Value "," Key 3 ":" Value "}

If the second argument is not specified, the value will be None. print(dict.fromkeys(l)) {3: None, 2: None, 1: None, 5: None, 4: None}```

##list() If you pass a dictionary as an argument, a list will be created from the key.

Key does not overlap ##Main subject input

l = [3, 3, 2, 1, 5, 1, 4, 2, 3]
print(dict.fromkeys(l))
print(list(dict.fromkeys(l)))

output

{3: None, 2: None, 1: None, 5: None, 4: None}`

[3, 2, 1, 5, 4]

Recommended Posts

How to remove duplicates from a Python list while preserving order.
[Python] How to convert a 2D list to a 1D list
How to open a web browser from python
How to clear tuples in a list (Python)
How to generate a Python object from JSON
How to remove duplicate elements in Python3 list
[Python] How to use list 1
How to write a list / dictionary type of Python3
[Python] How to output the list values in order
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
[Python] How to make a list of character strings character by character
How to slice a block multiple array from a multiple array in Python
How to run a Python program from within a shell script
How to shuffle a part of a Python list (at random.shuffle)
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to get a list of links from a page from wikipedia
Extract the value closest to a value from a Python list element
How to write a Python class
python / Make a dict from a list.
[Python] How to use list 3 Added
How to access wikipedia from python
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
How to get a string from a command line argument in python
Pass a list by reference from Python to C ++ with pybind11
How to delete multiple specified positions (indexes) in a Python list
How to create a clone from Github
[Python] How to make a class iterable
Send a message from Python to Slack
[Python] How to invert a character string
How to get a stacktrace in python
How to access RDS from Lambda (python)
How to create a repository from media
Summary of how to use Python list
How to run a Maya Python script
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to get a value from a parameter store in lambda (using python)
How to format a list of dictionaries (or instances) well in Python
[Discord.py] How to add or remove job titles after a reaction [python]
Send a message from Slack to a Python server
[Python] List Comprehension Various ways to create a list
[python] How to display list elements side by side
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
Let Code Day 62 "83. Remove Duplicates from Sorted List"
How to create a Python virtual environment (venv)
How to embed a variable in a python string
How to create a function object from a string
Study from Python Hour7: How to use classes
How to create a JSON file in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
How to add a Python module search path
How to handle Linux commands well from Python
How to extract coefficients from a fractional formula
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
Sort list elements in a specified order in Python
[Python] How to easily drop a child process started by multiprocess from another process
How to get a list of files in the same directory with python