Assign function output to empty list constant in python

I found something like this in the code I'm working with:

[], my_variable = my_function(a, b) 

where the output of my_function is as follows:

 return some_dict, some_list 

It seems to work - unit system tests don't crash, but when I try this in the python console (assigning the dictionary “[]”), it raises:

 ValueError: too many values to unpack 

Have you seen something like this? How to assign a dictionary (or something else) to remove the list constant "[]" to work?

Or is it not, and the tests are missing something ...

+5
source share
2 answers

This is because there is {} in your test file return value .

 >>> [] = {} >>> [] = {1:2} Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack >>> [] = [] >>> [] = [1,2] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack 
+2
source

You cannot assign a list because it is not a variable; you can use the list syntax to assign multiple variables:

 [a, b, c] = [1, 2, 3] 

Will just assign 1 a, etc.

What you do can be expressed by this expression:

 [] = {"a":1} 

What python is trying to do is map the number of elements in the dictionary to the number of elements in the list. It finds a mismatch and causes an error. You could do this:

 [x] = {"a":1} 
+1
source

All Articles