[Python](EN) Remove duplicated elements in list
Post about removing duplicated elements in Python list
Environment and Prerequisite
- Python
Remove Duplicated Elements
- Insert to dictionary as key values and change those to list again.
- By following below methods inserted order in list is preserved.
- Different versions have different methods, so they should be used accordingly. Related things are in Reference.
Python 2.7 or higher
OrderedDict.fromkeys(iterable[, value])
- Returns a dictionary using the given values as key values. If there is a
value
, thevalue
is set as value for all key values, otherwise it isNone
. OrderedDict
: Available starting with Python 2.7- Inserted elements order in
iterable
is preserved
Example
ex_list = [3, 4, 5, 6, 10, 1, 4, 5, 7, 8, 1, 5, 9]
from collections import OrderedDict
result = list(OrderedDict.fromkeys(ex_list))
print(result)
[3, 4, 5, 6, 10, 1, 7, 8, 9]
Example
ex_list = ['e', 'e', 'b', 'c', 'a', 'e', 'd', 'd', 'b', 'a']
from collections import OrderedDict
result = list(OrderedDict.fromkeys(ex_list))
print(result)
['e', 'b', 'c', 'a', 'd']
Python 3.7 or higher
dict.fromkeys(iterable[, value])
- Returns a dictionary using the given values as key values. If there is a
value
, thevalue
is set as value for all key values, otherwise it isNone
. - Inserted elements order in
iterable
is preserved starting with Python 3.7 - Partially supported in 3.6 implementation
Example
ex_list = [3, 4, 5, 6, 10, 1, 4, 5, 7, 8, 1, 5, 9]
result = list(dict.fromkeys(ex_list))
print(result)
[3, 4, 5, 6, 10, 1, 7, 8, 9]
Example
ex_list = ['e', 'e', 'b', 'c', 'a', 'e', 'd', 'd', 'b', 'a']
result = list(dict.fromkeys(ex_list))
print(result)
['e', 'b', 'c', 'a', 'd']
Reference
- https://stackoverflow.com/questions/7961363/removing-duplicates-in-the-lists
- https://docs.python.org/release/2.6/search.html?q=OrderedDict&check_keywords=yes&area=default
- https://docs.python.org/release/2.7/search.html?q=OrderedDict&check_keywords=yes&area=default
- https://www.python.org/dev/peps/pep-0372/
- https://docs.python.org/3/whatsnew/3.6.html#summary-release-highlights
- https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights