[Python] how to generate value for python defaultdict based on the key

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    In python, is it possible to generate a value for a defaultdict which is a function of the key? For example:

    from collections import defaultdict
    d = defaultdict(lambda key=None: key * 2)


    so that d[1] will produces the value 2.

    Continue reading...

Compartilhe esta Página