Python Forum
Check if an duplicate exits in a list
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if an duplicate exits in a list
#1
Hello everyone, I am new to python and also new here. I have written a code to check if an duplicate exits in a list but it gives an error. Can anyone help me with what I am doing wrong.
# Changable list downwards
itemlist=['boy','cat','dog','man','man','dog','dog']
# Changable list upwards
from time import sleep
streak=1
tcvar=0
error=0
length=len(itemlist)
while length>0:
    test=itemlist[0]
    listsize=length-1
    tcvar=1
    while not tcvar==length:
        testx=itemlist[tcvar]
        if testx==test:
            print('Match found-{}-in value{}'.format(test,tcvar+streak))
            del itemlist[tcvar+streak-1]
            sleep(1.5)
            error=1
        tcvar+=1
    del itemlist[0]
    length=len(itemlist)
    streak+=1
if error==1:
    print('Process finished with errors')
else:
    print('Process finished without errors')
[size=x-large]
[/size]
Reply
#2
Your code looks really over-complicated.

you can use set to remove duplicates
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> set(itemlist)
set(['boy', 'man', 'dog', 'cat'])
>>> list(set(itemlist))
['boy', 'man', 'dog', 'cat']
If your trying to actually obtain which are duplicates
>>> import collections
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> [item for item, count in collections.Counter(itemlist).items() if count > 1] 
['man', 'dog']
or grab the number of dups
>>> import collection
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> collections.Counter(itemlist).items() 
[('boy', 1), ('man', 2), ('dog', 3), ('cat', 1)]
Recommended Tutorials:
Reply
#3
(Jan-31-2017, 02:24 PM)metulburr Wrote: Your code looks really over-complicated.

you can use set to remove duplicates
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> set(itemlist)
set(['boy', 'man', 'dog', 'cat'])
>>> list(set(itemlist))
['boy', 'man', 'dog', 'cat']
If your trying to actually obtain which are duplicates
>>> import collections
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> [item for item, count in collections.Counter(itemlist).items() if count > 1] 
['man', 'dog']
or grab the number of dups
>>> import collection
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> collections.Counter(itemlist).items() 
[('boy', 1), ('man', 2), ('dog', 3), ('cat', 1)]
Thanks, these modules are completely new to me as I have just started learning. Though I am quite confused why my code fails.
It fails on qpython3 for android.
The faulty line is
del itemlist[tcver+streak-1]
It gives an out of index error.
Yes I am removing the duplicates from the list so that it does not calculates 'dog' three times.

Though your code solves my problem it would be better if I know where I am going wrong.

Thanks
Reply
#4
Quote:tcver+streak-1
then whatever index this is does not exist at the time of the traceback

Quote:Yes I am removing the duplicates from the list so that it does not calculates 'dog' three times.

Though your code solves my problem it would be better if I know where I am going wrong.
No offense, but your program is the reason why there are built-ins such as set(). Its meant for tedious repetitive tasks. And your code is quite all over the place and hard to read. And if its hard to read, then it is easy for bugs to fit in.

Even if you wanted to remove duplicates manually, there is a much easier to read method such as using "in" operator.

def remove_duplicates(seq):
    newlist = []
    for elem in seq:
       if elem not in newlist:
           newlist.append(elem)
    return newlist
itemlist=['boy','cat','dog','man','man','dog','dog']
print(remove_duplicates(itemlist))
Output:
['boy', 'cat', 'dog', 'man']
Recommended Tutorials:
Reply
#5
(Jan-31-2017, 03:03 PM)metulburr Wrote: Even if you wanted to remove duplicates manually, there is a much easier to read method such as using "in" operator.

def remove_duplicates(numbers):
    newlist = []
    for number in numbers:
       if number not in newlist:
           newlist.append(number)
    return newlist
print(remove_duplicates([1,2,3,4,5,5,5,6,3,2, 'tup','tup']))
Output:
[1, 2, 3, 4, 5, 6, 'tup']
Thanks, seems like a much better fit for what I want to achive.
Reply
#6
Well, if you want to remove the duplicated items from a list, in addition to set() you can use the list's methods.

In [1]: l = ['boy','cat','dog','man','man','dog','dog']

In [2]: for item in l:
   ...:     if l.count(item) > 1:
   ...:         l.remove(item)
   ...:         print(l)
   ...:         
['boy', 'cat', 'man', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog']
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Jan-31-2017, 03:19 PM)wavic Wrote: Well, if you want to remove the duplicated items from a list, in addition to set() you can use the list's methods.

In [1]: l = ['boy','cat','dog','man','man','dog','dog']

In [2]: for item in l:
   ...:     if l.count(item) > 1:
   ...:         l.remove(item)
   ...:         print(l)
   ...:         
['boy', 'cat', 'man', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog']

Thanks wavic for another great method. Now I'm confused which one to use.
Reply
#8
Doesn't matter if the list is small. If it's not well, don't know which is faster
More important is to learn the basics. The data types in Python and their methods, the loops, functions and classes... A few more thighs.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
In my humble opinion, the set method is the best.
Reply
#10
(Jan-31-2017, 03:45 PM)wavic Wrote: Doesn't matter if the list is small. If it's not well, don't know which is faster
More important is to learn the basics. The data types in Python and their methods, the loops, functions and classes... A few more thighs.
Thanks, I'm still learning, even not from a teacher, just by online tutorials and some books. Hope I would have better understanding someday.
(Jan-31-2017, 03:47 PM)Larz60+ Wrote: In my humble opinion, the set method is the best.
That is the technique I'm using currently alongside with the one with 'in' check. Thanks for your opinion.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 403 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 684 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  why globals() exits python when quit() is aliased as q abc12346 4 687 May-23-2023, 08:56 AM
Last Post: Gribouillis
  check if element is in a list in a dictionary value ambrozote 4 1,964 May-11-2022, 06:05 PM
Last Post: deanhystad
  How to check if a list is in another list finndude 4 1,836 Jan-17-2022, 05:04 PM
Last Post: bowlofred
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,488 Aug-12-2021, 04:25 PM
Last Post: palladium
  how to check if string contains ALL words from the list? zarize 6 7,209 Jul-22-2020, 07:04 PM
Last Post: zarize
  Removing duplicate list items eglaud 4 2,687 Nov-22-2019, 08:07 PM
Last Post: ichabod801
  removing duplicate numbers from a list calonia 12 5,248 Jun-16-2019, 12:09 PM
Last Post: DeaD_EyE
  check how many times an item appears in list davidm 8 5,011 Dec-29-2018, 07:49 PM
Last Post: davidm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020