0

In order not to use so many lines to declare variables like this:

open = []

high = []

low = []

close = []

Could I do something like this:

open,high,low,close = []

That actually works

3
  • 1
    That last line should not work. How did it work for you? The last line unpacks whatever is in the right hand side, so your list should have had four items to unpack Commented Jun 14, 2017 at 2:11
  • How will it not throw an error? Commented Jun 14, 2017 at 2:15
  • 1
    Me too, in both py27 and py3 and it produces ValueError: need more than 0 values to unpack in py27 and ValueError: not enough values to unpack (expected 4, got 0) in py35. Commented Jun 14, 2017 at 2:19

1 Answer 1

4

No. The example in your question will not work. It will raise a ValueError. This is because Python is attempting to unpack four values into open, high, low, and closed, but if finds no valuest(this will raise an error in both Python 2.x and Python 3.x):

>>> open, high, low, close = []
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 4, got 0)
>>> 

To initialize all of the lists variables on the same line, you need to provide a list for all four variables:

>>> open, high, low, closed = [], [], [], []
>>> 
>>> open
[]
>>> high
[]
>>> low
[]
>>> closed
[]
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.