import secrets


def gen_pass(length, use_spaces, wordlist):
    """Generate a secure passphrase

    Params:
    - length (int)
        The length of the password to generate (in number of words)
    - use_spaces (bool)
        Generate passphrase with or without spaces
    - wordlist (list)
        A list of individual words

    Returns:
    - string
    """
    phrase = ""
    last_word = False
    for i in range (0, length):
        if i == length - 1:
            last_word = True
        index = secrets.randbelow(len(wordlist) - 1)
        phrase += wordlist[index]
        if not last_word and use_spaces:
            phrase += " "

    return phrase