0

Look at the code below:

def f1(l):
  l[0] = 'row'
  l[1] = 'col'
  return True
def f2():
  l = [0,0]
  f1(l)
  print(l)
f2()

running the f2 function, prints l = ['row', 'col']. Why did this happen?

My Python version is 3.7.13

  • 1
    What do you think should have happened instead, and why? – Scott Hunter Apr 12 '22 at 23:47
  • Because you mutated the dict. What else did you expect to happen? – juanpa.arrivillaga Apr 13 '22 at 00:11
  • @ScottHunter @juanpa.arrivillaga I gueseed `l = [0,0]` because I didn't return the list from `f1`. I think that `l ` could be `['row', 'col']` in 2 situations: 1) When I've defined `l` globally and 2) when `f1` returns a list and in `f2`, I have this line: `l = f1(l)` – Shaghayegh L Apr 14 '22 at 12:43

0 Answers0