Agony
Agony

Reputation: 27

TypeError: '>' not supported between instances of 'list' and 'int' Python

Working on something where I need to generate 1000 random elements, then also generate a single random element called "searched value" then pass it through binary search and print weather the "searched value" was found or not... but getting an error

Code:

def binarySearch(item_list,item):
    first = 0
    last = len(item_list)-1
    found = False
    while( first<=last and not found):
        mid = (first + last)//2
        if item_list[mid] == item :
            found = True
        else:
            if item < item_list[mid]:
                last = mid - 1
            else:
                first = mid + 1
    return found

arr = [random.randrange(9999) for x in range(1000)]
searched_value = [random.randrange(0,1000)]
print(binarySearch(arr, searched_value))

Errors:

line 19, in <module>
    print(binarySearch(arr, searched_value))

line 11, in binarySearch
    if item < item_list[mid]:

TypeError: '<' not supported between instances of 'list' and 'int'

Upvotes: 0

Views: 1371

Answers (2)

Sennin Modo
Sennin Modo

Reputation: 36

This line of code here

searched_value = [random.randrange(0,1000)]

should be

searched_value = random.randrange(0, 1000)

As it is you are creating a list and not a random number

Upvotes: 2

yaho cho
yaho cho

Reputation: 1779

searched_value = [random.randrange(0,1000)]

===>

searched_value = random.randrange(0,1000)

Because, searched_value was list but, item_listpmid] is int.

Upvotes: 1

Related Questions