SoFunction
Updated on 2024-11-18

Python implementation of the removal of duplicate elements in the list of the method summary [4 methods

This article example describes the Python implementation of removing duplicate elements in the list. Shared for your reference, as follows:

Here a total of four methods are used to remove duplicate elements in the list, the following is the specific implementation:

#!usr/bin/env python
#encoding:utf-8
'''
__Author__:Yishui Chancheng
Function: Remove duplicate elements from a list
'''
def func1(one_list):
  '''''
  Using Collections, Personally Most Used
  '''
  return list(set(one_list))
def func2(one_list):
  '''''
  Ways to use a dictionary
  '''
  return {}.fromkeys(one_list).keys()
def func3(one_list):
  '''''
  Using list derivation
  '''
  temp_list=[]
  for one in one_list:
    if one not in temp_list:
      temp_list.append(one)
  return temp_list
def func4(one_list):
  '''''
  Using the sorting method
  '''
  result_list=[]
  temp_list=sorted(one_list)
  i=0
  while i<len(temp_list):
    if temp_list[i] not in result_list:
      result_list.append(temp_list[i])
    else:
      i+=1
  return result_list
if __name__ == '__main__':
  one_list=[56,7,4,23,56,9,0,56,12,3,56,34,45,5,6,56]
  print "I test results:"
  print func1(one_list)
  print func2(one_list)
  print func3(one_list)
  print func4(one_list)

The results are as follows:

I test results:
[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]
[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]
[56, 7, 4, 23, 9, 0, 12, 3, 34, 45, 5, 6]
[0, 3, 4, 5, 6, 7, 9, 12, 23, 34, 45, 56]

Screenshots of the run results:

PS: There are two relatively simple and practical online text to repeat the tool, recommended for everyone to use:

Online duplicate item removal tool:
http://tools./code/quchong

Online text de-duplication tool:
http://tools./aideddesign/txt_quchong

More about Python related content can be viewed on this site's topic: theSummary of Python dictionary manipulation techniques》、《Summary of Python string manipulation techniques》、《Summary of common Python traversal techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tipsand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.