[Python] How to print variable name in a list

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    I just want to print the variable's name as in the case below:

    Andi = 100
    Bill = 50
    Kira = 90
    list1 = [Andi, Bill, Kira]
    for i in list1:
    print(i)


    How can I have the variable name and value printed together?

    This solution was shown in a now deleted answer, and there was a comment saying using eval is bad practice:

    Andi = 100
    Bill = 50
    Kira = 90
    list1 = ['Andi', 'Bill', 'Kira']
    for i in list1:
    print(i, eval(i))


    Is there a better way?

    Continue reading...

Compartilhe esta Página