How to use in-built bisect to find the index of an item in a list

75 views Asked by At
D = [(20832049, "hello", 3), (2042449014, "bye", 2), (208414004814, "cya", 3)

I want to make a function with the arguments:

(key, D, hash)

where key represents the element at index[1] at each tuple, D is the dictionary example I gave, and hash is just to hash the key.

My aim is to create a function which finds the value of a tuple if there is a tuple in the list which has the same key or hash(key)

for example, if I did

get("hello", D, hash)

The function would return the value "3"

The list is already sorted

1

There are 1 answers

1
UnsignedByte On

You can use this function:

def get(key, D, hash):
    for tuple in D:
        tuplelist = tuple.strip("()").split(", ")
        return (tuplelist[2] if tuplelist[1] == key)