Python Forum
Duplicated words in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Duplicated words in a list
#1
Hi!
I have a task:

Write a function called has_duplicates that takes a list and returns True if there is any element that appears more than once. It should not modify the original list.


Here is my attempt but I dont know why I do fail! Sick Cry
def has_dup(v):
    c=0
    
    for i in range(len(v)):
        for j in range(len(v)-1):
            if v[i]==v[j+1]:
                c=c+1
                print(c)
    if c>1:
        return True
    if c<=1:
        return False
If v=['car','bar','are']
Then has_dup(v) must be False but it gives me True every time ! why? Huh
v[0] is not the same as v[1] or v[2], v[1] is not either the same like v[2], so why it increases the value of c ?
Reply


Messages In This Thread
Duplicated words in a list - by pooyan89 - Jun-15-2019, 12:47 PM
RE: Duplicated words in a list - by ThomasL - Jun-15-2019, 12:58 PM
RE: Duplicated words in a list - by pooyan89 - Jun-15-2019, 12:59 PM
RE: Duplicated words in a list - by ThomasL - Jun-15-2019, 01:03 PM
RE: Duplicated words in a list - by Yoriz - Jun-15-2019, 01:24 PM
RE: Duplicated words in a list - by pooyan89 - Jun-15-2019, 01:27 PM
RE: Duplicated words in a list - by noisefloor - Jun-15-2019, 06:18 PM
RE: Duplicated words in a list - by perfringo - Jun-15-2019, 08:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  capitalizing words in list Truman 1 3,535 Feb-19-2018, 11:40 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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