JenkinsMa
JenkinsMa

Reputation: 225

Python: check if last value of list occurs more than once

Problem:

Write a program that will search through a list to see if the last value in the list occurs more than once. If the last value occurs more than once, return true. Otherwise, return false. If the list is empty, return false.

My Code:

def go(list1):
for i in range(0,len(list1)):
    if list1[i] is list1[-1:]:
        return True
else:
    return False

print ( go( [-99,1,2,3,4,5,6,7,8,9,10,5] ) )
print ( go( [10,9,8,7,6,5,4,3,2,1,9] ) )
print ( go( [10,20,30,40,50,-11818,40,30,20,10] ) )
print ( go( [32767] ) )
print ( go( [7,7,7,7] ) )
print ( go( [9,10,-88,100,-555,1000] ) )
print ( go( [10,10,10,11,456,10,10] ) )
print ( go( [-111,1,2,3,9,11,20,30] ) )
print ( go( [9,8,7,6,5,4,3,2,0,-2,9,9] ) )
print ( go( [12,15,18,21,23,1000] ) )
print ( go( [250,19,17,15,13,11,10,9,6,3,2,1,250] ) )
print ( go( [] ) )

My Output:

False
False
False
False
False
False
False
False
False
False
False
False

Desired Output:

True
True
True
False
True
False
True
False
True
False
True
False

What am I doing wrong? Why am I getting False for all my outputs?

Upvotes: 3

Views: 2181

Answers (3)

Vasilis G.
Vasilis G.

Reputation: 7859

Your mistake is that you're trying to compare an element with a slice of list. Another mistake is that you're using the keyword is instead of ==. In order to check whether the last element exists more than once in the list, you need to check if the current element is equal with the last element and their indices are different (which means that there more than one elements with the same value). My recommendation below:

def go(list1):
    length = len(list1)
    for i in range(0,length):
        if list1[i]==list1[-1] and not i==length-1:
            return True
    else:
        return False

print ( go( [-99,1,2,3,4,5,6,7,8,9,10,5] ) )
print ( go( [10,9,8,7,6,5,4,3,2,1,9] ) )
print ( go( [10,20,30,40,50,-11818,40,30,20,10] ) )
print ( go( [32767] ) )
print ( go( [7,7,7,7] ) )
print ( go( [9,10,-88,100,-555,1000] ) )
print ( go( [10,10,10,11,456,10,10] ) )
print ( go( [-111,1,2,3,9,11,20,30] ) )
print ( go( [9,8,7,6,5,4,3,2,0,-2,9,9] ) )
print ( go( [12,15,18,21,23,1000] ) )
print ( go( [250,19,17,15,13,11,10,9,6,3,2,1,250] ) )
print ( go( [] ) )

Output:

True
True
True
False
True
False
True
False
True
False
True
False

Upvotes: 1

user3483203
user3483203

Reputation: 51185

You should not be using is here, because is does not check for equality of values, but instead for equality of references.

Instead you can use arr[-1] in arr[:-1] to see if the final value of your list is present anywhere else in the list.

x = [-99,1,2,3,4,5,6,7,8,9,10,5] 

def is_last_contained(arr):
  return arr[-1] in arr[:-1]

print(is_last_contained(x))

Output:

True

Upvotes: 6

sushain97
sushain97

Reputation: 2812

There are a couple errors in your code as written:

  1. range(0,len(list1)) should be range(0,len(list1)-1) since you don't want to compare the last element against itself.
  2. list1[-1:] should be list1[-1]. The former selects a slice, the latter a single element.

That being said, @chrisz's solution is more Pythonic and concise :)

Upvotes: 2

Related Questions