Replacing data in lists

Asked

Viewed 16 times

0

def substituir(lista,velho,novo):
    lista = [1,2,3,3,5]
    resultado = substituir(lista[velho],novo)
    return resultado 

The problem is that I can’t replace an old list item with a new one.

For example if I put substituir(lista, 2, 99)

Indices with value 2 would be replaced with the value 99

Example:

Before:

lista = [1,2,3,2,4]

Afterward:

[1,99,3,99,4] 
  • Why do you redefine lista within the function?

  • because only some data would be the same value, not all

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.