import torch


def torch_onehot(cats: list[str], key: str) -> torch.Tensor:
    """
    Generate the onehot tensor of given categories and key.
    Example
    -------
        cats: ['apple', ''banana', 'orange']
        key: ['banana']
    Return:
        torch.Tensor([[0., 1., 0.]])
    :param cats: list of categories
    :param key: key
    :return: tensor of onehot representation
    """
    try:
        i = cats.index(key)
    except IndexError:
        raise IndexError(f'Error reading genres. The genre ({key}) I got is not in the list ({cats}). ')
    else:
        result = torch.zeros(1, len(cats))
        result[0, i] = 1
        return result