[Python] How do I merge two dictionaries in a single expression in Python?

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    I want to merge two dictionaries into a new dictionary.

    x = {'a': 1, 'b': 2}
    y = {'b': 3, 'c': 4}
    z = merge(x, y)

    >>> z
    {'a': 1, 'b': 3, 'c': 4}


    Whenever a key k is present in both dictionaries, only the value y[k] should be kept.

    Continue reading...

Compartilhe esta Página