[Python] Pythonic way to append function output to existing lists?

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have two functions that each return a tuple of lists. What is the most pythonic way to merge them? I am using tmp vars at the moment, which I find a bit awkward. Is there maybe a one-line-solution?

    Here is my existing code, reduced to the bare minimum:

    def check_this() -> Tuple[List[str], List[str]]:
    problems = []
    warnings = []
    # do some tests to fill those lists with various error messages
    return problems, warnings

    def check_that() -> Tuple[List[str], List[str]]:
    problems = []
    warnings = []
    # do some more tests to fill those lists with various error messages
    return problems, warnings

    def log_check_results(problems: List[str], warnings: List[str]):
    logging.error("\n".join(problems))
    logging.warning("\n".join(warnings))


    I do not want to merge the check methods because they check completely different aspects of the project.

    This was my first approach:

    problems, warnings = check_this()
    p, w = check_that()
    problems.extend(p)
    warnings.extend(w)
    log_check_results(problems, warnings)


    I have tried the following but find it harder to read (hence the question for a "pythonic" solution.)

    problems, warnings = zip(check_this(), check_that())
    log_check_results([m for l in problems for m in l],
    [m for l in warnings for m in l])


    Any better ideas?

    Continue reading...

Compartilhe esta Página