def extract_entities(input_data_tokens, entity_dict):
    """Extracts valid entities present in the input query.

    Parses the tokenized input list to find valid entity values, based
    on the given entity dataset.

    Args:
        input_data_tokens: A list of string tokens, without any punctuation,
            based on the input string.
        entity_dict: A dictionary of dictionary, of entity values for a
            particular entity type.

    Returns:
        A list of valid entity values and their start, stop token index
        locations in the tokenized input query.

        [(['comedy', 'action'], 5, 7), (['suspense'], 9, 10)]

        Always returns a list. If no valid entities are detected, returns
        an empty list.
    """
    detected_entities = []
    length = len(input_data_tokens)
    for i, word in enumerate(input_data_tokens):
        if word in entity_dict:
            start = i
            stop = -1
            loc = i  # keeps track of the current cursor posiiton
            current_dict = entity_dict
            # keeps track of the current dictionary data

            while(loc <= length and current_dict):
                if 1 in current_dict:
                    # tag index of a potential entity value if a
                    # longer entity is not present
                    stop = loc

                    if len(current_dict) == 1:
                        detected_entities.append(
                            (input_data_tokens[start:stop], start, stop)
                        )
                        stop = -1  # reset

                # if end of query reached or mismatch in entity values,
                # discard and move on to the next word
                if loc == length or input_data_tokens[loc] not in current_dict:
                    # save a shorter entity, if it exists in the already \
                    # parsed query
                    if stop != -1:
                        detected_entities.append(
                            (input_data_tokens[start:stop], start, stop)
                        )
                    break
                else:
                    # entity matches up until current word, continue
                    current_dict = current_dict[input_data_tokens[loc]]
                    loc += 1

    return detected_entities