{"id":18494,"date":"2021-12-10T16:20:11","date_gmt":"2021-12-10T10:50:11","guid":{"rendered":"https:\/\/java2blog.com\/?p=18494"},"modified":"2023-11-25T17:34:37","modified_gmt":"2023-11-25T12:04:37","slug":"valueerror-too-many-values-to-unpack-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/valueerror-too-many-values-to-unpack-python\/","title":{"rendered":"[Fixed] ValueError: too many values to unpack (expected 2)"},"content":{"rendered":"\n<p>While programming in python, we can assign values to two or more variables in a single statement. While doing so, you might get the <code>ValueError<\/code> exception with the message \u201c<code>ValueError: too many values to unpack<\/code> \u201d. If you are trying to assign values to two variables at the same time, you will get the message \u201c<code>ValueError: too many values to unpack (expected 2)<\/code>\u201d. In this article, we will see why this exception occurs and how we can resolve it.<\/p>\n\n\n\n<p>[toc]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is unpacking in python?<\/h2>\n\n\n\n<p>When we have a container object like a list, tuple, set, or dictionary, we can assign the elements of the container object directly to variables. Suppose that you have a list containing n elements. You can assign the elements to n variables in a single statement as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1,var2,var3,var4...., varN=&#91;element1,element2,element3,element4..., elementN]<\/code><\/pre>\n\n\n\n<p>This process of assigning the elements of a container object to variables is called unpacking in python. In other words, we unpack the elements from the container and assign them to different variables. You can observe this in the following example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myList = &#91;1, 2, 3, 4]\nprint(\"The list is:\", myList)\nvar1, var2, var3, var4 = myList\nprint(\"var1 is:\", var1)\nprint(\"var2 is:\", var2)\nprint(\"var3 is:\", var3)\nprint(\"var4 is:\", var4)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The list is: &#91;1, 2, 3, 4]\nvar1 is: 1\nvar2 is: 2\nvar3 is: 3\nvar4 is: 4<\/code><\/pre>\n\n\n\n<p>In the above example, we have a list named <code>myList<\/code> having 4 elements in it. In the expression<code> var1, var2, var3, var4 = myList <\/code>, we have unpacked all the elements of the list into var1,var2,var3, and var4. The elements of the list are assigned to the variables in their respective order. i.e. The first variable gets the first element, the second variable gets the second element, and so on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is ValueError: too many values to unpack (expected 2)?<\/h2>\n\n\n\n<p>While unpacking, we have to make sure that the number of variables should be equal to the number of elements in the container object. However, if we unpack a container object and assign it to fewer variables than the elements present <code>in the container object, it will raise the ValueError exception with the message <\/code>ValueError: too many values to unpack. For instance, if we have a list with four elements and we assign it only to three elements, it will raise the <code>ValueError<\/code> exception as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myList = &#91;1, 2, 3, 4]\nprint(\"The list is:\", myList)\nvar1, var2, var3= myList\nprint(\"var1 is:\", var1)\nprint(\"var2 is:\", var2)\nprint(\"var3 is:\", var3)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The list is: &#91;1, 2, 3, 4]\nTraceback (most recent call last):\n  File \"\/home\/aditya1117\/PycharmProjects\/pythonProject\/string1.py\", line 3, in &lt;module&gt;\n    var1, var2, var3= myList\nValueError: too many values to unpack (expected 3)<\/code><\/pre>\n\n\n\n<p>You can observe that in the case of a lesser number of variables than the elements in the container object, the program raises the <code>ValueError<\/code> exception. For instance, we have tried to assign a list with more elements to three variables <code>var1<\/code>, <code>var2<\/code>, and <code>var3<\/code>. This results in an error with the message <code>ValueError: too many values to unpack (expected 3)<\/code>.<\/p>\n\n\n\n<p> If we assign the elements of a list to only two variables and the list happens to contain more than two elements, it will raise the <code>ValueError<\/code> exception with the message <code>ValueError: too many values to unpack (expected 2)<\/code> as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myList = &#91;1, 2, 3, 4]\nprint(\"The list is:\", myList)\nvar1, var2= myList\nprint(\"var1 is:\", var1)\nprint(\"var2 is:\", var2)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The list is: &#91;1, 2, 3, 4]\nTraceback (most recent call last):\n  File \"\/home\/aditya1117\/PycharmProjects\/pythonProject\/string1.py\", line 3, in &lt;module&gt;\n    var1, var2= myList\nValueError: too many values to unpack (expected 2)<\/code><\/pre>\n\n\n\n<p>Similarly, if we assign the elements of a tuple, dictionary, or set to two variables and the number of elements is more than 2, the program will raise the <code>ValueError<\/code> exception with the message <code>ValueError: too many values to unpack (expected 2)<\/code>.<\/p>\n\n\n\n<p>One more situation where this kind of <code>ValueError<\/code> exception occurs when we work with dictionaries. Generally, beginners think that we can extract keys and values from a dictionary and print them as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myDict = {\"name\": \"Java2blog\", \"website\": \"java2blog.com\", \"language\": \"python\"}\nfor key, value in myDict:\n    print(key)\n    print(value)<\/code><\/pre>\n\n\n\n<p>However, this will lead to the ValueError exception as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Traceback (most recent call last):\n  File \"\/home\/aditya1117\/PycharmProjects\/pythonProject\/string1.py\", line 2, in &lt;module&gt;\n    for key, value in myDict:\nValueError: too many values to unpack (expected 2)<\/code><\/pre>\n\n\n\n<p>Here, the user thinks that in each iteration of the for loop, a key and a value from <code>myDict<\/code> will be assigned to the variable <code>key<\/code> and the variable <code>value<\/code>. However, this doesn&#8217;t happen. In the for loop, <code>myDict<\/code> works as an iterable object that has the keys of the dictionary in it. In the first execution of the for loop, the string &#8220;<code>name<\/code>&#8221;  from <code>myDict<\/code> is assigned to the variables <code>key<\/code> and <code>value<\/code> in the for loop. As there are four characters in the string &#8220;<code>name<\/code>&#8220;, the unpacking operation fails and the exception is raised with the message<code> ValueError: too many values to unpack (expected 2)<\/code>. Had there been only two characters in the keys of the dictionary, the above <code>for loop <\/code>will run without any error. However, it won&#8217;t produce the desired result. You can observe this in the following example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myDict = {\"na\": \"Java2blog\", \"ws\": \"java2blog.com\", \"ln\": \"python\"}\nfor key, value in myDict:\n    print(key)\n    print(value)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n\na\nw\ns\nl\nn<\/code><\/pre>\n\n\n\n<p>In the above output, you can observe that the variables <code>key<\/code> and <code>value<\/code> in the for loop are assigned characters from the strings in the keys of the dictionary. They are not assigned the actual keys and values of the dictionary. Hence, the above code makes no sense, although it executes without any error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to resolve ValueError: too many values to unpack (expected 2)?<\/h2>\n\n\n\n<p>To avoid the <code>ValueError<\/code> exception with the message \u201c<code>ValueError: too many values to unpack (expected 2)<\/code>\u201d, you should make sure that you should use the same number of variables as the elements in the container object while unpacking. For instance, if you have two variables, you should only unpack a container object with only two elements as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myList = &#91;1, 2]\nprint(\"The list is:\", myList)\nvar1, var2 = myList\nprint(\"var1 is:\", var1)\nprint(\"var2 is:\", var2)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The list is: &#91;1, 2]\nvar1 is: 1\nvar2 is: 2<\/code><\/pre>\n\n\n\n<p>While working with dictionaries, you should use the <code>items()<\/code> method to iterate through the keys and values of the dictionary as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myDict = {\"name\": \"Java2blog\", \"website\": \"java2blog.com\", \"language\": \"python\"}\nfor key, value in myDict.items():\n    print(key)\n    print(value)\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name\nJava2blog\nwebsite\njava2blog.com\nlanguage\npython<\/code><\/pre>\n\n\n\n<p>Here, the <code>items()<\/code> method returns a list of tuples. Each tuple contains two elements where the first element is a key of the dictionary and the second element of the tuple is the associated value. Hence, In each iteration of the for loop, a tuple is unpacked and the values are assigned to the variables key and value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we discussed why we get the error with the message <code>ValueError: too many values to unpack (expected 2)<\/code> in python. We also discussed how we can avoid this error in our program.&nbsp;To make sure that you don&#8217;t run into this error, you just have to unpack the container objects to the same number of variables as the number of elements in the container object like list, set, or tuple.<\/p>\n\n\n\n<p>I hope you enjoyed reading this article. Stay tuned for more informative articles.<\/p>\n\n\n\n<p>Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While programming in python, we can assign values to two or more variables in a single statement. While doing so, you might get the ValueError exception with the message \u201cValueError: too many values to unpack \u201d. If you are trying to assign values to two variables at the same time, you will get the message [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":18586,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[145,177],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18494"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=18494"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18494\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/18586"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=18494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=18494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=18494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}