Python four ways to remove duplicate elements from a list

Source: Internet
Author: User

Reprinted: 76201975

  1. def func1 (one_list):
  2. "' "
  3. Use collections, which are most commonly used by individuals
  4. ‘‘‘
  5. return list (set (one_list))
  6. def func2 (one_list):
  7. "' "
  8. How to use a dictionary
  9. ‘‘‘
  10. return {}.fromkeys (one_list). Keys ()
  11. def func3 (one_list):
  12. "' "
  13. How to Use list deduction
  14. ‘‘‘
  15. Temp_list=[]
  16. For one in one_list:
  17. If one not in temp_list:
  18. Temp_list.append (one)
  19. return temp_list
  20. def func4 (one_list):
  21. "' "
  22. Ways to use sorting
  23. ‘‘‘
  24. Result_list=[]
  25. Temp_list=sorted (One_list)
  26. i=0
  27. While I<len (temp_list):
  28. if temp_list[i] not in result_list:
  29. Result_list.append (Temp_list[i])
  30. Else:
  31. i+=1
  32. return result_list
  33. if __name__ = = ' __main__ ':
  34. one_list=[,7,4,3, 9, 0,, 5, +, 6, +]
  35. print func1 (one_list)
  36. print Func2 (one_list)
  37. print func3 (one_list)
  38. print Func4 (one_list)

Python four ways to remove duplicate elements from a list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.