0
def example():
    var1 = {'a':1,'b':2}
    var2 = {'c':3,'d':4}

    return var1,var2

[v1, v2] = example()

I want to assign var1 to v1, and var2 to v2. Is it ok for me to unpacked tuple var1,var2 into list v1,v2. So far, I haven't found anyone unpack the multiple return value into list

Aran-Fey
  • 30,995
  • 8
  • 80
  • 121
  • refer to this [question](https://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python) – Benyamin Jafari Sep 25 '18 at 13:29
  • 3
    Possible duplicate of [How do you return multiple values in Python?](https://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python) – Benyamin Jafari Sep 25 '18 at 13:29

1 Answers1

7

Semantically, there is no difference between unpacking into a list or a tuple. All of these are equivalent:

v1, v2 = example()
(v1, v2) = example()
[v1, v2] = example()

(See also the assignment statement grammar.)

However, unpacking into a list is a relatively unknown feature, so it might be confusing for people who read your code. It's also needlessly verbose. That's why I would strongly recommend using the well-known unpacking syntax without any parentheses or brackets:

v1, v2 = example()
Aran-Fey
  • 30,995
  • 8
  • 80
  • 121