[Python] Changing values in list: Python

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

  1. Stack

    Stack Membro Participativo

    I'm making a function to modify the elements in a list, but it doesn't change all the way through... My function is:

    def modifyValues(l):
    for x in l:
    if x == 1:
    l[x] = 'a'
    elif x == 2:
    l[x] = 'b'
    elif x == 3:
    l[x] = 'c'
    print (l)


    when

    modifyValues([1, 2, 3, 2, 3, 1, 2, 2])


    the output is:

    ['a', 'b', 'c', 2, 3, 1, 2, 2]


    Why doesn't it change every value?

    Continue reading...

Compartilhe esta Página