2

Is there a way to make arr = [5,4,3,2,1] while still using both functions and without making arr global? I need to pass multiple values back from y() to x(), but I don't want arr to = [5,4,3,[2,1]]. Or do I need to redesign my functions?

arr = [5,4,3]

def x():
    arr.append(y())

def y():
    a = 2
    b = 1
    newArr = [a,b]
    return newArr

x()
print arr
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Adam12344
  • 1,043
  • 16
  • 33

1 Answers1

4

You're using the wrong method:

def x():
    arr.extend(y())

arr.append(thing) means "add thing as a new item on the end of arr". arr.extend(thing) means "add all of thing's contents to the end of arr".

user2357112
  • 260,549
  • 28
  • 431
  • 505