

Due to this, the program runs into a Python ValueError exception saying that there are too many values to unpack. In the about code, you an observe that there are six elements in the list but we have only five variables. ValueError: too many values to unpack (expected 5) ValueError Traceback (most recent call last) You can observe this in the following example. Otherwise, the program will run into error.įor instance, if the number of variables on the left-hand side is less than the number of elements in the iterable object, the program will run into a ValueError exception saying that there are too many values to unpack. In the above syntax, the number of variables must be equal to the number of elements in the iterable_object. Hence, we have unpacked the list into six variables a, b, c, d, e, and f. In the above example, we have six elements in myList. After execution of the statement, all the variables are initialized with elements from iterable_object as shown below. In the above statement, variables var1, var2, var3, var4 till varN are individual variables.

var1, var2, var3,var4.varN=iterable_object For this, you can use the following syntax. Instead of using the * operator, you can unpack an iterable object into multiple variables using parallel assignment. Unpacking in Python Using Parallel Assignment Hence, the program runs into Synta圎rror exception. In this example, we have tried to assign elements from myList to six variables using the * operator. Synta圎rror: can't use starred expression here If you do so, the program will run into an error. Remember that you cannot use the unpacking operator to assign the elements of the iterable object to individual elements. Then, we created a set from the unpacked elements. In the above example, the * operator unpacks myList. To understand this, consider the following example. Then, we can use the packing operation to create other iterable objects. After execution of the above statement, the elements of iterable_object are unpacked.The iterable_object variable represents an iterable object such as a list, tuple, set, or a Python dictionary.
