# Math Help - Invalid syntax in Python

1. ## Invalid syntax in Python

Can someone tell me what the syntax error in this program is? The program attempts to check a list for duplicates and return True if it has any, False if it has none.

def dupl(L):
----if len(L) <= 1:
--------return False
----elif L[0] in L[1:]:
--------return True
----else:
--------return dupl(L[1:])

The syntax error is marked in bold.

2. Originally Posted by quiney
Can someone tell me what the syntax error in this program is? The program attempts to check a list for duplicates and return True if it has any, False if it has none.

def dupl(L):
----if len(L) <= 1:
--------return False
----elif L[0] in L[1:]:
--------return True
----else:
--------return dupl(L[1:])

The syntax error is marked in bold.
Works perfectly fine on my computer. Not sure why you would be getting a syntax error.

I'm assuming those hyphens are just in order to preserve indentation on this forum. You can use [code][/code] tags, like so:

Code:
def dupl(L):
if len(L) <= 1:
return False
elif L[0] in L[1:]:
return True
else:
return dupl(L[1:])

L1 = [1,2,3,4,5]
L2 = [1,2,3,2,5]

if dupl(L1):
print "hello"
if dupl(L2):
print "goodbye"
Output:

Code:
goodbye

3. Thanks. The program works fine now. I have one more question. Would something like the following program accomplish what I'm trying to do?

Code:
def dup(L):
i=1
dup=False
if len(L)==0:
return dup
else:
while i<=len(L)-1:
if sorted(L)[i]==sorted(L)[i-1]:
dup=True
else:
i=i+1
return dup
When I run this program, for some lists, it returns the correct answer, but for others, the program freezes up.

4. Originally Posted by quiney
Thanks. The program works fine now. I have one more question. Would something like the following program accomplish what I'm trying to do?

Code:
def dup(L):
i=1
dup=False
if len(L)==0:
return dup
else:
while i<=len(L)-1:
if sorted(L)[i]==sorted(L)[i-1]:
dup=True
else:
i=i+1
return dup
When I run this program, for some lists, it returns the correct answer, but for others, the program freezes up.
Probably where you wrote dup=True you meant to write return True. What you have causes an infinite loop in the event of a duplicate.

Or maybe you meant to write a break statement, but might as well just return, it's simpler.

Instead of calling the sorted method so many times, why not just write L2 = sorted(L) near the beginning of the function?

Also after you replace dup=True with return True you'll see that the variable dup is superfluous.

5. Thanks! So far I have this:
Code:
def dup(L):
i=1
L2=sorted(L)
if len(L)==0:
return False
else:
while i<=len(L)-1:
if L2[i]==L2[i-1]:
return True
else:
i=i+1
This returns True whenever there is a duplicate, but I still don't know how to get it to return false when there is no duplicate and the list is greater than 0.

6. Originally Posted by quiney
Thanks! So far I have this:

This returns True whenever there is a duplicate, but I still don't know how to get it to return false when there is no duplicate and the list is greater than 0.
I took the liberty of changing a few things around. Compare, and use whatever aspects of it you like

Code:
def dupl(L):
if len(L)<2:
return False
i=0
L2=sorted(L)
while i<len(L)-1:
if L2[i]==L2[i+1]:
return True
i=i+1
return False

L1 = [1,2,3,4,5]
L2 = [1,2,3,2,5]

if dupl(L1):
print "hello"
if dupl(L2):
print "goodbye"
Note in particular that a "return" can render an "else" unnecessary.

7. Thanks. I also figured out a much shorter way to do it:
Code:
def dupl(L):
if len(L)==len(set(L)):
return False
return True

8. Originally Posted by quiney
Thanks. I also figured out a much shorter way to do it:
Code:
def dupl(L):
if len(L)==len(set(L)):
return False
return True
Nice! I come from a non-Python background so I have not seen this before, but it makes perfect sense. However, you can save some keystrokes as follows:

Code:
def dupl(L):
return len(L)!=len(set(L))