Identify duplicate values in a list in Python

前端 未结 12 1924
渐次进展
渐次进展 2020-11-29 06:31

Is it possible to get which values are duplicates in a list using python?

I have a list of items:

    mylist = [20, 30, 25, 20]

I k

12条回答
  •  被撕碎了的回忆
    2020-11-29 07:02

    That's the simplest way I can think for finding duplicates in a list:

    my_list = [3, 5, 2, 1, 4, 4, 1]
    
    my_list.sort()
    for i in range(0,len(my_list)-1):
                   if my_list[i] == my_list[i+1]:
                       print str(my_list[i]) + ' is a duplicate'
    

提交回复
热议问题