1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Transform a list of dictionaries, based on key list

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    In Python 3.9, I have a list of dictionaries:

    variables = [
    {'id': ['alpha'], 'ip': '10.10.10.10', 'name': 'primary'},
    {'id': ['beta', 'gamma'], 'ip': '10.10.10.20', 'name': 'secondary'}
    ]


    My goal is to transform it into this dictionary format:

    result = {
    'alpha.ip': '10.10.10.10',
    'alpha.name': 'primary',
    'beta.ip': '10.10.10.20',
    'beta.name': 'secondary',
    'gamma.ip': '10.10.10.20',
    'gamma.name': 'secondary'
    }


    I have a difficult time drafting the id loop logic, which will produce the correct result.

    Continue reading...

Compartilhe esta Página