[{"data":1,"prerenderedAt":2003},["ShallowReactive",2],{"doc-\u002Ferrors\u002Ftypeerror-cannot-unpack-non-iterable-object-fix":3},{"id":4,"title":5,"body":6,"description":1996,"extension":1997,"meta":1998,"navigation":86,"path":1999,"seo":2000,"stem":2001,"__hash__":2002},"content\u002Ferrors\u002Ftypeerror-cannot-unpack-non-iterable-object-fix.md","TypeError: cannot unpack non-iterable object (Fix)",{"type":7,"value":8,"toc":1964},"minimark",[9,13,17,37,42,175,178,190,193,197,200,235,238,255,262,281,293,300,304,307,324,327,351,355,358,375,381,384,411,414,422,426,429,453,455,464,472,475,491,494,507,510,514,517,522,567,569,584,588,631,633,645,649,652,724,726,738,745,749,752,755,794,797,817,820,824,827,888,890,921,924,930,999,1001,1032,1035,1054,1057,1078,1085,1089,1092,1096,1162,1169,1173,1226,1228,1246,1250,1330,1332,1357,1361,1364,1451,1454,1457,1537,1539,1557,1564,1568,1571,1605,1608,1671,1674,1731,1734,1757,1763,1767,1770,1787,1790,1798,1801,1815,1822,1826,1830,1843,1847,1850,1852,1889,1891,1900,1904,1910,1914,1924,1928,1960],[10,11,5],"h1",{"id":12},"typeerror-cannot-unpack-non-iterable-object-fix",[14,15,16],"p",{},"This error happens when Python tries to split one value into multiple variables, but that value is not something Python can unpack.",[14,18,19,20,24,25,28,29,32,33,36],{},"For example, Python can unpack a tuple like ",[21,22,23],"code",{},"(1, 2)"," into ",[21,26,27],{},"a"," and ",[21,30,31],{},"b",". But it cannot unpack a single integer like ",[21,34,35],{},"5",".",[38,39,41],"h2",{"id":40},"quick-fix","Quick fix",[43,44,49],"pre",{"className":45,"code":46,"language":47,"meta":48,"style":48},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","value = 5\n# Wrong:\n# a, b = value\n\n# Fix 1: assign to one variable\nnumber = value\n\n# Fix 2: unpack an actual iterable\npair = (1, 2)\na, b = pair\nprint(a, b)\n","python","",[21,50,51,68,75,81,88,94,105,110,116,140,155],{"__ignoreMap":48},[52,53,56,60,64],"span",{"class":54,"line":55},"line",1,[52,57,59],{"class":58},"su5hD","value ",[52,61,63],{"class":62},"smGrS","=",[52,65,67],{"class":66},"srdBf"," 5\n",[52,69,71],{"class":54,"line":70},2,[52,72,74],{"class":73},"sutJx","# Wrong:\n",[52,76,78],{"class":54,"line":77},3,[52,79,80],{"class":73},"# a, b = value\n",[52,82,84],{"class":54,"line":83},4,[52,85,87],{"emptyLinePlaceholder":86},true,"\n",[52,89,91],{"class":54,"line":90},5,[52,92,93],{"class":73},"# Fix 1: assign to one variable\n",[52,95,97,100,102],{"class":54,"line":96},6,[52,98,99],{"class":58},"number ",[52,101,63],{"class":62},[52,103,104],{"class":58}," value\n",[52,106,108],{"class":54,"line":107},7,[52,109,87],{"emptyLinePlaceholder":86},[52,111,113],{"class":54,"line":112},8,[52,114,115],{"class":73},"# Fix 2: unpack an actual iterable\n",[52,117,119,122,124,128,131,134,137],{"class":54,"line":118},9,[52,120,121],{"class":58},"pair ",[52,123,63],{"class":62},[52,125,127],{"class":126},"sP7_E"," (",[52,129,130],{"class":66},"1",[52,132,133],{"class":126},",",[52,135,136],{"class":66}," 2",[52,138,139],{"class":126},")\n",[52,141,143,145,147,150,152],{"class":54,"line":142},10,[52,144,27],{"class":58},[52,146,133],{"class":126},[52,148,149],{"class":58}," b ",[52,151,63],{"class":62},[52,153,154],{"class":58}," pair\n",[52,156,158,162,165,168,170,173],{"class":54,"line":157},11,[52,159,161],{"class":160},"sptTA","print",[52,163,164],{"class":126},"(",[52,166,27],{"class":167},"slqww",[52,169,133],{"class":126},[52,171,172],{"class":167}," b",[52,174,139],{"class":126},[14,176,177],{},"Output:",[43,179,181],{"className":45,"code":180,"language":47,"meta":48,"style":48},"1 2\n",[21,182,183],{"__ignoreMap":48},[52,184,185,187],{"class":54,"line":55},[52,186,130],{"class":66},[52,188,189],{"class":66}," 2\n",[14,191,192],{},"Unpacking only works when the value on the right side is an iterable, such as a tuple, list, or string, and it contains the expected number of items.",[38,194,196],{"id":195},"what-this-error-means","What this error means",[14,198,199],{},"This error means:",[201,202,203,207,210],"ul",{},[204,205,206],"li",{},"Python tried to split one value into multiple variables.",[204,208,209],{},"The value was not iterable, so Python could not unpack it.",[204,211,212,213],{},"Common non-iterable values include:\n",[201,214,215,220,225,230],{},[204,216,217],{},[21,218,219],{},"int",[204,221,222],{},[21,223,224],{},"float",[204,226,227],{},[21,228,229],{},"bool",[204,231,232],{},[21,233,234],{},"None",[14,236,237],{},"A very common pattern that causes this is:",[43,239,241],{"className":45,"code":240,"language":47,"meta":48,"style":48},"a, b = value\n",[21,242,243],{"__ignoreMap":48},[52,244,245,247,249,251,253],{"class":54,"line":55},[52,246,27],{"class":58},[52,248,133],{"class":126},[52,250,149],{"class":58},[52,252,63],{"class":62},[52,254,104],{"class":58},[14,256,257,258,261],{},"This only works if ",[21,259,260],{},"value"," is something like:",[201,263,264,269,275],{},[204,265,266,267],{},"a tuple: ",[21,268,23],{},[204,270,271,272],{},"a list: ",[21,273,274],{},"[1, 2]",[204,276,277,278],{},"a string: ",[21,279,280],{},"\"hi\"",[14,282,283,284,286,287,289,290,36],{},"If ",[21,285,260],{}," is just one number like ",[21,288,35],{},", Python raises a ",[21,291,292],{},"TypeError",[14,294,295,296,36],{},"If you are not familiar with iterables, see ",[27,297,299],{"href":298},"\u002Flearn\u002Fiterators-and-iterable-objects-explained\u002F","iterators and iterable objects explained",[38,301,303],{"id":302},"when-this-error-happens","When this error happens",[14,305,306],{},"You will usually see this error in one of these situations:",[201,308,309,315,318,321],{},[204,310,311,312],{},"Using code like ",[21,313,314],{},"a, b = 10",[204,316,317],{},"Expecting a function to return multiple values, but it returns one value",[204,319,320],{},"Looping with unpacking over items that are not pairs or groups",[204,322,323],{},"Reading data and assuming each item has multiple parts",[14,325,326],{},"Common causes include:",[201,328,329,332,338,341,344],{},[204,330,331],{},"Trying to unpack an integer into multiple variables",[204,333,334,335,337],{},"Trying to unpack ",[21,336,234],{}," returned from a function",[204,339,340],{},"Using a loop that expects pairs but the data contains single values",[204,342,343],{},"Forgetting that a function returns one value, not multiple values",[204,345,346,347,350],{},"Mixing up dictionary iteration with dictionary ",[21,348,349],{},".items()"," iteration",[38,352,354],{"id":353},"example-that-causes-the-error","Example that causes the error",[14,356,357],{},"Here is a simple example:",[43,359,361],{"className":45,"code":360,"language":47,"meta":48,"style":48},"a, b = 5\n",[21,362,363],{"__ignoreMap":48},[52,364,365,367,369,371,373],{"class":54,"line":55},[52,366,27],{"class":58},[52,368,133],{"class":126},[52,370,149],{"class":58},[52,372,63],{"class":62},[52,374,67],{"class":66},[14,376,377,378,380],{},"This fails because ",[21,379,35],{}," is a single integer, not a list or tuple.",[14,382,383],{},"You will get an error like this:",[43,385,387],{"className":45,"code":386,"language":47,"meta":48,"style":48},"TypeError: cannot unpack non-iterable int object\n",[21,388,389],{"__ignoreMap":48},[52,390,391,394,397,400,403,406,408],{"class":54,"line":55},[52,392,292],{"class":393},"sZMiF",[52,395,396],{"class":126},":",[52,398,399],{"class":58}," cannot unpack non",[52,401,402],{"class":62},"-",[52,404,405],{"class":58},"iterable ",[52,407,219],{"class":393},[52,409,410],{"class":393}," object\n",[14,412,413],{},"The important part is the same in all versions of this error:",[201,415,416,419],{},[204,417,418],{},"Python expected something unpackable",[204,420,421],{},"It got a non-iterable value instead",[38,423,425],{"id":424},"fix-1-assign-to-one-variable","Fix 1: assign to one variable",[14,427,428],{},"If the value is just one item, use one variable.",[43,430,432],{"className":45,"code":431,"language":47,"meta":48,"style":48},"number = 5\nprint(number)\n",[21,433,434,442],{"__ignoreMap":48},[52,435,436,438,440],{"class":54,"line":55},[52,437,99],{"class":58},[52,439,63],{"class":62},[52,441,67],{"class":66},[52,443,444,446,448,451],{"class":54,"line":70},[52,445,161],{"class":160},[52,447,164],{"class":126},[52,449,450],{"class":167},"number",[52,452,139],{"class":126},[14,454,177],{},[43,456,458],{"className":45,"code":457,"language":47,"meta":48,"style":48},"5\n",[21,459,460],{"__ignoreMap":48},[52,461,462],{"class":54,"line":55},[52,463,457],{"class":66},[14,465,466,467,471],{},"Use this fix when you do ",[468,469,470],"strong",{},"not"," actually need unpacking.",[14,473,474],{},"Wrong:",[43,476,477],{"className":45,"code":360,"language":47,"meta":48,"style":48},[21,478,479],{"__ignoreMap":48},[52,480,481,483,485,487,489],{"class":54,"line":55},[52,482,27],{"class":58},[52,484,133],{"class":126},[52,486,149],{"class":58},[52,488,63],{"class":62},[52,490,67],{"class":66},[14,492,493],{},"Right:",[43,495,497],{"className":45,"code":496,"language":47,"meta":48,"style":48},"number = 5\n",[21,498,499],{"__ignoreMap":48},[52,500,501,503,505],{"class":54,"line":55},[52,502,99],{"class":58},[52,504,63],{"class":62},[52,506,67],{"class":66},[14,508,509],{},"This is the simplest solution when your code only has one value.",[38,511,513],{"id":512},"fix-2-return-or-use-an-iterable","Fix 2: return or use an iterable",[14,515,516],{},"If you want multiple variables, the value on the right side must be an iterable with the right number of items.",[518,519,521],"h3",{"id":520},"unpacking-a-tuple","Unpacking a tuple",[43,523,525],{"className":45,"code":524,"language":47,"meta":48,"style":48},"a, b = (1, 2)\nprint(a)\nprint(b)\n",[21,526,527,547,557],{"__ignoreMap":48},[52,528,529,531,533,535,537,539,541,543,545],{"class":54,"line":55},[52,530,27],{"class":58},[52,532,133],{"class":126},[52,534,149],{"class":58},[52,536,63],{"class":62},[52,538,127],{"class":126},[52,540,130],{"class":66},[52,542,133],{"class":126},[52,544,136],{"class":66},[52,546,139],{"class":126},[52,548,549,551,553,555],{"class":54,"line":70},[52,550,161],{"class":160},[52,552,164],{"class":126},[52,554,27],{"class":167},[52,556,139],{"class":126},[52,558,559,561,563,565],{"class":54,"line":77},[52,560,161],{"class":160},[52,562,164],{"class":126},[52,564,31],{"class":167},[52,566,139],{"class":126},[14,568,177],{},[43,570,572],{"className":45,"code":571,"language":47,"meta":48,"style":48},"1\n2\n",[21,573,574,579],{"__ignoreMap":48},[52,575,576],{"class":54,"line":55},[52,577,578],{"class":66},"1\n",[52,580,581],{"class":54,"line":70},[52,582,583],{"class":66},"2\n",[518,585,587],{"id":586},"unpacking-a-list","Unpacking a list",[43,589,591],{"className":45,"code":590,"language":47,"meta":48,"style":48},"a, b = [10, 20]\nprint(a, b)\n",[21,592,593,617],{"__ignoreMap":48},[52,594,595,597,599,601,603,606,609,611,614],{"class":54,"line":55},[52,596,27],{"class":58},[52,598,133],{"class":126},[52,600,149],{"class":58},[52,602,63],{"class":62},[52,604,605],{"class":126}," [",[52,607,608],{"class":66},"10",[52,610,133],{"class":126},[52,612,613],{"class":66}," 20",[52,615,616],{"class":126},"]\n",[52,618,619,621,623,625,627,629],{"class":54,"line":70},[52,620,161],{"class":160},[52,622,164],{"class":126},[52,624,27],{"class":167},[52,626,133],{"class":126},[52,628,172],{"class":167},[52,630,139],{"class":126},[14,632,177],{},[43,634,636],{"className":45,"code":635,"language":47,"meta":48,"style":48},"10 20\n",[21,637,638],{"__ignoreMap":48},[52,639,640,642],{"class":54,"line":55},[52,641,608],{"class":66},[52,643,644],{"class":66}," 20\n",[518,646,648],{"id":647},"returning-multiple-values-from-a-function","Returning multiple values from a function",[14,650,651],{},"A function that should provide multiple values can return a tuple.",[43,653,655],{"className":45,"code":654,"language":47,"meta":48,"style":48},"def get_coordinates():\n    return (3, 7)\n\nx, y = get_coordinates()\nprint(x, y)\n",[21,656,657,670,688,692,709],{"__ignoreMap":48},[52,658,659,663,667],{"class":54,"line":55},[52,660,662],{"class":661},"sbsja","def",[52,664,666],{"class":665},"sGLFI"," get_coordinates",[52,668,669],{"class":126},"():\n",[52,671,672,676,678,681,683,686],{"class":54,"line":70},[52,673,675],{"class":674},"sVHd0","    return",[52,677,127],{"class":126},[52,679,680],{"class":66},"3",[52,682,133],{"class":126},[52,684,685],{"class":66}," 7",[52,687,139],{"class":126},[52,689,690],{"class":54,"line":77},[52,691,87],{"emptyLinePlaceholder":86},[52,693,694,697,699,702,704,706],{"class":54,"line":83},[52,695,696],{"class":58},"x",[52,698,133],{"class":126},[52,700,701],{"class":58}," y ",[52,703,63],{"class":62},[52,705,666],{"class":167},[52,707,708],{"class":126},"()\n",[52,710,711,713,715,717,719,722],{"class":54,"line":90},[52,712,161],{"class":160},[52,714,164],{"class":126},[52,716,696],{"class":167},[52,718,133],{"class":126},[52,720,721],{"class":167}," y",[52,723,139],{"class":126},[14,725,177],{},[43,727,729],{"className":45,"code":728,"language":47,"meta":48,"style":48},"3 7\n",[21,730,731],{"__ignoreMap":48},[52,732,733,735],{"class":54,"line":55},[52,734,680],{"class":66},[52,736,737],{"class":66}," 7\n",[14,739,740,741,36],{},"If this part is unclear, you may want to read ",[27,742,744],{"href":743},"\u002Flearn\u002Freturn-values-in-python-functions\u002F","return values in Python functions",[38,746,748],{"id":747},"fix-3-check-what-a-function-really-returns","Fix 3: check what a function really returns",[14,750,751],{},"Sometimes the unpacking code looks correct, but the function returns something different from what you expected.",[14,753,754],{},"For example:",[43,756,758],{"className":45,"code":757,"language":47,"meta":48,"style":48},"def get_total():\n    return 100\n\na, b = get_total()\n",[21,759,760,769,776,780],{"__ignoreMap":48},[52,761,762,764,767],{"class":54,"line":55},[52,763,662],{"class":661},[52,765,766],{"class":665}," get_total",[52,768,669],{"class":126},[52,770,771,773],{"class":54,"line":70},[52,772,675],{"class":674},[52,774,775],{"class":66}," 100\n",[52,777,778],{"class":54,"line":77},[52,779,87],{"emptyLinePlaceholder":86},[52,781,782,784,786,788,790,792],{"class":54,"line":83},[52,783,27],{"class":58},[52,785,133],{"class":126},[52,787,149],{"class":58},[52,789,63],{"class":62},[52,791,766],{"class":167},[52,793,708],{"class":126},[14,795,796],{},"This raises:",[43,798,799],{"className":45,"code":386,"language":47,"meta":48,"style":48},[21,800,801],{"__ignoreMap":48},[52,802,803,805,807,809,811,813,815],{"class":54,"line":55},[52,804,292],{"class":393},[52,806,396],{"class":126},[52,808,399],{"class":58},[52,810,402],{"class":62},[52,812,405],{"class":58},[52,814,219],{"class":393},[52,816,410],{"class":393},[14,818,819],{},"The function returns one integer, not two values.",[518,821,823],{"id":822},"debug-it-first","Debug it first",[14,825,826],{},"Print the result before unpacking it:",[43,828,830],{"className":45,"code":829,"language":47,"meta":48,"style":48},"def get_total():\n    return 100\n\nresult = get_total()\nprint(result)\nprint(type(result))\n",[21,831,832,840,846,850,861,872],{"__ignoreMap":48},[52,833,834,836,838],{"class":54,"line":55},[52,835,662],{"class":661},[52,837,766],{"class":665},[52,839,669],{"class":126},[52,841,842,844],{"class":54,"line":70},[52,843,675],{"class":674},[52,845,775],{"class":66},[52,847,848],{"class":54,"line":77},[52,849,87],{"emptyLinePlaceholder":86},[52,851,852,855,857,859],{"class":54,"line":83},[52,853,854],{"class":58},"result ",[52,856,63],{"class":62},[52,858,766],{"class":167},[52,860,708],{"class":126},[52,862,863,865,867,870],{"class":54,"line":90},[52,864,161],{"class":160},[52,866,164],{"class":126},[52,868,869],{"class":167},"result",[52,871,139],{"class":126},[52,873,874,876,878,881,883,885],{"class":54,"line":96},[52,875,161],{"class":160},[52,877,164],{"class":126},[52,879,880],{"class":393},"type",[52,882,164],{"class":126},[52,884,869],{"class":167},[52,886,887],{"class":126},"))\n",[14,889,177],{},[43,891,893],{"className":45,"code":892,"language":47,"meta":48,"style":48},"100\n\u003Cclass 'int'>\n",[21,894,895,900],{"__ignoreMap":48},[52,896,897],{"class":54,"line":55},[52,898,899],{"class":66},"100\n",[52,901,902,905,908,912,915,918],{"class":54,"line":70},[52,903,904],{"class":62},"\u003C",[52,906,907],{"class":661},"class",[52,909,911],{"class":910},"sjJ54"," '",[52,913,219],{"class":914},"s_sjI",[52,916,917],{"class":910},"'",[52,919,920],{"class":62},">\n",[14,922,923],{},"Now the problem is easy to see.",[518,925,927,928],{"id":926},"another-common-case-returning-none","Another common case: returning ",[21,929,234],{},[43,931,933],{"className":45,"code":932,"language":47,"meta":48,"style":48},"def do_something():\n    print(\"Done\")\n\nresult = do_something()\nprint(result)\nprint(type(result))\n",[21,934,935,944,961,965,975,985],{"__ignoreMap":48},[52,936,937,939,942],{"class":54,"line":55},[52,938,662],{"class":661},[52,940,941],{"class":665}," do_something",[52,943,669],{"class":126},[52,945,946,949,951,954,957,959],{"class":54,"line":70},[52,947,948],{"class":160},"    print",[52,950,164],{"class":126},[52,952,953],{"class":910},"\"",[52,955,956],{"class":914},"Done",[52,958,953],{"class":910},[52,960,139],{"class":126},[52,962,963],{"class":54,"line":77},[52,964,87],{"emptyLinePlaceholder":86},[52,966,967,969,971,973],{"class":54,"line":83},[52,968,854],{"class":58},[52,970,63],{"class":62},[52,972,941],{"class":167},[52,974,708],{"class":126},[52,976,977,979,981,983],{"class":54,"line":90},[52,978,161],{"class":160},[52,980,164],{"class":126},[52,982,869],{"class":167},[52,984,139],{"class":126},[52,986,987,989,991,993,995,997],{"class":54,"line":96},[52,988,161],{"class":160},[52,990,164],{"class":126},[52,992,880],{"class":393},[52,994,164],{"class":126},[52,996,869],{"class":167},[52,998,887],{"class":126},[14,1000,177],{},[43,1002,1004],{"className":45,"code":1003,"language":47,"meta":48,"style":48},"Done\nNone\n\u003Cclass 'NoneType'>\n",[21,1005,1006,1011,1017],{"__ignoreMap":48},[52,1007,1008],{"class":54,"line":55},[52,1009,1010],{"class":58},"Done\n",[52,1012,1013],{"class":54,"line":70},[52,1014,1016],{"class":1015},"s39Yj","None\n",[52,1018,1019,1021,1023,1025,1028,1030],{"class":54,"line":77},[52,1020,904],{"class":62},[52,1022,907],{"class":661},[52,1024,911],{"class":910},[52,1026,1027],{"class":914},"NoneType",[52,1029,917],{"class":910},[52,1031,920],{"class":62},[14,1033,1034],{},"If you then try:",[43,1036,1038],{"className":45,"code":1037,"language":47,"meta":48,"style":48},"a, b = do_something()\n",[21,1039,1040],{"__ignoreMap":48},[52,1041,1042,1044,1046,1048,1050,1052],{"class":54,"line":55},[52,1043,27],{"class":58},[52,1045,133],{"class":126},[52,1047,149],{"class":58},[52,1049,63],{"class":62},[52,1051,941],{"class":167},[52,1053,708],{"class":126},[14,1055,1056],{},"You will get a similar error:",[43,1058,1060],{"className":45,"code":1059,"language":47,"meta":48,"style":48},"TypeError: cannot unpack non-iterable NoneType object\n",[21,1061,1062],{"__ignoreMap":48},[52,1063,1064,1066,1068,1070,1072,1075],{"class":54,"line":55},[52,1065,292],{"class":393},[52,1067,396],{"class":126},[52,1069,399],{"class":58},[52,1071,402],{"class":62},[52,1073,1074],{"class":58},"iterable NoneType ",[52,1076,1077],{"class":393},"object\n",[14,1079,1080,1081,36],{},"That usually means the function did not return the values you expected. This is related to ",[27,1082,1084],{"href":1083},"\u002Ferrors\u002Ftypeerror-nonetype-object-is-not-iterable-fix\u002F","TypeError: 'NoneType' object is not iterable",[38,1086,1088],{"id":1087},"fix-4-unpack-loop-items-correctly","Fix 4: unpack loop items correctly",[14,1090,1091],{},"This error often happens in loops.",[518,1093,1095],{"id":1094},"wrong-unpacking-single-numbers","Wrong: unpacking single numbers",[43,1097,1099],{"className":45,"code":1098,"language":47,"meta":48,"style":48},"data = [1, 2, 3]\n\nfor a, b in data:\n    print(a, b)\n",[21,1100,1101,1123,1127,1148],{"__ignoreMap":48},[52,1102,1103,1106,1108,1110,1112,1114,1116,1118,1121],{"class":54,"line":55},[52,1104,1105],{"class":58},"data ",[52,1107,63],{"class":62},[52,1109,605],{"class":126},[52,1111,130],{"class":66},[52,1113,133],{"class":126},[52,1115,136],{"class":66},[52,1117,133],{"class":126},[52,1119,1120],{"class":66}," 3",[52,1122,616],{"class":126},[52,1124,1125],{"class":54,"line":70},[52,1126,87],{"emptyLinePlaceholder":86},[52,1128,1129,1132,1135,1137,1139,1142,1145],{"class":54,"line":77},[52,1130,1131],{"class":674},"for",[52,1133,1134],{"class":58}," a",[52,1136,133],{"class":126},[52,1138,149],{"class":58},[52,1140,1141],{"class":674},"in",[52,1143,1144],{"class":58}," data",[52,1146,1147],{"class":126},":\n",[52,1149,1150,1152,1154,1156,1158,1160],{"class":54,"line":83},[52,1151,948],{"class":160},[52,1153,164],{"class":126},[52,1155,27],{"class":167},[52,1157,133],{"class":126},[52,1159,172],{"class":167},[52,1161,139],{"class":126},[14,1163,1164,1165,1168],{},"Each item in ",[21,1166,1167],{},"data"," is a single integer, not a pair. So this fails.",[518,1170,1172],{"id":1171},"right-use-one-loop-variable","Right: use one loop variable",[43,1174,1176],{"className":45,"code":1175,"language":47,"meta":48,"style":48},"data = [1, 2, 3]\n\nfor item in data:\n    print(item)\n",[21,1177,1178,1198,1202,1215],{"__ignoreMap":48},[52,1179,1180,1182,1184,1186,1188,1190,1192,1194,1196],{"class":54,"line":55},[52,1181,1105],{"class":58},[52,1183,63],{"class":62},[52,1185,605],{"class":126},[52,1187,130],{"class":66},[52,1189,133],{"class":126},[52,1191,136],{"class":66},[52,1193,133],{"class":126},[52,1195,1120],{"class":66},[52,1197,616],{"class":126},[52,1199,1200],{"class":54,"line":70},[52,1201,87],{"emptyLinePlaceholder":86},[52,1203,1204,1206,1209,1211,1213],{"class":54,"line":77},[52,1205,1131],{"class":674},[52,1207,1208],{"class":58}," item ",[52,1210,1141],{"class":674},[52,1212,1144],{"class":58},[52,1214,1147],{"class":126},[52,1216,1217,1219,1221,1224],{"class":54,"line":83},[52,1218,948],{"class":160},[52,1220,164],{"class":126},[52,1222,1223],{"class":167},"item",[52,1225,139],{"class":126},[14,1227,177],{},[43,1229,1231],{"className":45,"code":1230,"language":47,"meta":48,"style":48},"1\n2\n3\n",[21,1232,1233,1237,1241],{"__ignoreMap":48},[52,1234,1235],{"class":54,"line":55},[52,1236,578],{"class":66},[52,1238,1239],{"class":54,"line":70},[52,1240,583],{"class":66},[52,1242,1243],{"class":54,"line":77},[52,1244,1245],{"class":66},"3\n",[518,1247,1249],{"id":1248},"right-use-pairs-if-you-want-unpacking","Right: use pairs if you want unpacking",[43,1251,1253],{"className":45,"code":1252,"language":47,"meta":48,"style":48},"data = [(1, 2), (3, 4), (5, 6)]\n\nfor a, b in data:\n    print(a, b)\n",[21,1254,1255,1296,1300,1316],{"__ignoreMap":48},[52,1256,1257,1259,1261,1264,1266,1268,1270,1273,1275,1277,1279,1282,1284,1286,1288,1290,1293],{"class":54,"line":55},[52,1258,1105],{"class":58},[52,1260,63],{"class":62},[52,1262,1263],{"class":126}," [(",[52,1265,130],{"class":66},[52,1267,133],{"class":126},[52,1269,136],{"class":66},[52,1271,1272],{"class":126},"),",[52,1274,127],{"class":126},[52,1276,680],{"class":66},[52,1278,133],{"class":126},[52,1280,1281],{"class":66}," 4",[52,1283,1272],{"class":126},[52,1285,127],{"class":126},[52,1287,35],{"class":66},[52,1289,133],{"class":126},[52,1291,1292],{"class":66}," 6",[52,1294,1295],{"class":126},")]\n",[52,1297,1298],{"class":54,"line":70},[52,1299,87],{"emptyLinePlaceholder":86},[52,1301,1302,1304,1306,1308,1310,1312,1314],{"class":54,"line":77},[52,1303,1131],{"class":674},[52,1305,1134],{"class":58},[52,1307,133],{"class":126},[52,1309,149],{"class":58},[52,1311,1141],{"class":674},[52,1313,1144],{"class":58},[52,1315,1147],{"class":126},[52,1317,1318,1320,1322,1324,1326,1328],{"class":54,"line":83},[52,1319,948],{"class":160},[52,1321,164],{"class":126},[52,1323,27],{"class":167},[52,1325,133],{"class":126},[52,1327,172],{"class":167},[52,1329,139],{"class":126},[14,1331,177],{},[43,1333,1335],{"className":45,"code":1334,"language":47,"meta":48,"style":48},"1 2\n3 4\n5 6\n",[21,1336,1337,1343,1350],{"__ignoreMap":48},[52,1338,1339,1341],{"class":54,"line":55},[52,1340,130],{"class":66},[52,1342,189],{"class":66},[52,1344,1345,1347],{"class":54,"line":70},[52,1346,680],{"class":66},[52,1348,1349],{"class":66}," 4\n",[52,1351,1352,1354],{"class":54,"line":77},[52,1353,35],{"class":66},[52,1355,1356],{"class":66}," 6\n",[518,1358,1360],{"id":1359},"dictionary-example","Dictionary example",[14,1362,1363],{},"A common mistake is iterating over a dictionary like this:",[43,1365,1367],{"className":45,"code":1366,"language":47,"meta":48,"style":48},"person = {\"name\": \"Ana\", \"age\": 20}\n\nfor key, value in person:\n    print(key, value)\n",[21,1368,1369,1412,1416,1435],{"__ignoreMap":48},[52,1370,1371,1374,1376,1379,1381,1384,1386,1388,1391,1394,1396,1398,1400,1403,1405,1407,1409],{"class":54,"line":55},[52,1372,1373],{"class":58},"person ",[52,1375,63],{"class":62},[52,1377,1378],{"class":126}," {",[52,1380,953],{"class":910},[52,1382,1383],{"class":914},"name",[52,1385,953],{"class":910},[52,1387,396],{"class":126},[52,1389,1390],{"class":910}," \"",[52,1392,1393],{"class":914},"Ana",[52,1395,953],{"class":910},[52,1397,133],{"class":126},[52,1399,1390],{"class":910},[52,1401,1402],{"class":914},"age",[52,1404,953],{"class":910},[52,1406,396],{"class":126},[52,1408,613],{"class":66},[52,1410,1411],{"class":126},"}\n",[52,1413,1414],{"class":54,"line":70},[52,1415,87],{"emptyLinePlaceholder":86},[52,1417,1418,1420,1423,1425,1428,1430,1433],{"class":54,"line":77},[52,1419,1131],{"class":674},[52,1421,1422],{"class":58}," key",[52,1424,133],{"class":126},[52,1426,1427],{"class":58}," value ",[52,1429,1141],{"class":674},[52,1431,1432],{"class":58}," person",[52,1434,1147],{"class":126},[52,1436,1437,1439,1441,1444,1446,1449],{"class":54,"line":83},[52,1438,948],{"class":160},[52,1440,164],{"class":126},[52,1442,1443],{"class":167},"key",[52,1445,133],{"class":126},[52,1447,1448],{"class":167}," value",[52,1450,139],{"class":126},[14,1452,1453],{},"This is wrong because looping over a dictionary normally gives you one key at a time.",[14,1455,1456],{},"The correct version is:",[43,1458,1460],{"className":45,"code":1459,"language":47,"meta":48,"style":48},"person = {\"name\": \"Ana\", \"age\": 20}\n\nfor key, value in person.items():\n    print(key, value)\n",[21,1461,1462,1498,1502,1523],{"__ignoreMap":48},[52,1463,1464,1466,1468,1470,1472,1474,1476,1478,1480,1482,1484,1486,1488,1490,1492,1494,1496],{"class":54,"line":55},[52,1465,1373],{"class":58},[52,1467,63],{"class":62},[52,1469,1378],{"class":126},[52,1471,953],{"class":910},[52,1473,1383],{"class":914},[52,1475,953],{"class":910},[52,1477,396],{"class":126},[52,1479,1390],{"class":910},[52,1481,1393],{"class":914},[52,1483,953],{"class":910},[52,1485,133],{"class":126},[52,1487,1390],{"class":910},[52,1489,1402],{"class":914},[52,1491,953],{"class":910},[52,1493,396],{"class":126},[52,1495,613],{"class":66},[52,1497,1411],{"class":126},[52,1499,1500],{"class":54,"line":70},[52,1501,87],{"emptyLinePlaceholder":86},[52,1503,1504,1506,1508,1510,1512,1514,1516,1518,1521],{"class":54,"line":77},[52,1505,1131],{"class":674},[52,1507,1422],{"class":58},[52,1509,133],{"class":126},[52,1511,1427],{"class":58},[52,1513,1141],{"class":674},[52,1515,1432],{"class":58},[52,1517,36],{"class":126},[52,1519,1520],{"class":167},"items",[52,1522,669],{"class":126},[52,1524,1525,1527,1529,1531,1533,1535],{"class":54,"line":83},[52,1526,948],{"class":160},[52,1528,164],{"class":126},[52,1530,1443],{"class":167},[52,1532,133],{"class":126},[52,1534,1448],{"class":167},[52,1536,139],{"class":126},[14,1538,177],{},[43,1540,1542],{"className":45,"code":1541,"language":47,"meta":48,"style":48},"name Ana\nage 20\n",[21,1543,1544,1549],{"__ignoreMap":48},[52,1545,1546],{"class":54,"line":55},[52,1547,1548],{"class":58},"name Ana\n",[52,1550,1551,1554],{"class":54,"line":70},[52,1552,1553],{"class":58},"age ",[52,1555,1556],{"class":66},"20\n",[14,1558,1559,1560,36],{},"For more help with this pattern, see ",[27,1561,1563],{"href":1562},"\u002Fhow-to\u002Fhow-to-loop-through-a-dictionary-in-python\u002F","how to loop through a dictionary in Python",[38,1565,1567],{"id":1566},"how-to-debug-this-step-by-step","How to debug this step by step",[14,1569,1570],{},"When you see this error, use this checklist:",[1572,1573,1574,1593,1596,1599,1602],"ol",{},[204,1575,1576,1577],{},"Find the line with unpacking, usually something like:\n",[43,1578,1579],{"className":45,"code":240,"language":47,"meta":48,"style":48},[21,1580,1581],{"__ignoreMap":48},[52,1582,1583,1585,1587,1589,1591],{"class":54,"line":55},[52,1584,27],{"class":58},[52,1586,133],{"class":126},[52,1588,149],{"class":58},[52,1590,63],{"class":62},[52,1592,104],{"class":58},[204,1594,1595],{},"Print the value before that line.",[204,1597,1598],{},"Print its type.",[204,1600,1601],{},"Check whether it is iterable.",[204,1603,1604],{},"Check whether it contains the number of items you expect.",[14,1606,1607],{},"Useful debugging commands:",[43,1609,1611],{"className":45,"code":1610,"language":47,"meta":48,"style":48},"print(value)\nprint(type(value))\nprint(result)\nprint(type(result))\nprint(data)\n",[21,1612,1613,1623,1637,1647,1661],{"__ignoreMap":48},[52,1614,1615,1617,1619,1621],{"class":54,"line":55},[52,1616,161],{"class":160},[52,1618,164],{"class":126},[52,1620,260],{"class":167},[52,1622,139],{"class":126},[52,1624,1625,1627,1629,1631,1633,1635],{"class":54,"line":70},[52,1626,161],{"class":160},[52,1628,164],{"class":126},[52,1630,880],{"class":393},[52,1632,164],{"class":126},[52,1634,260],{"class":167},[52,1636,887],{"class":126},[52,1638,1639,1641,1643,1645],{"class":54,"line":77},[52,1640,161],{"class":160},[52,1642,164],{"class":126},[52,1644,869],{"class":167},[52,1646,139],{"class":126},[52,1648,1649,1651,1653,1655,1657,1659],{"class":54,"line":83},[52,1650,161],{"class":160},[52,1652,164],{"class":126},[52,1654,880],{"class":393},[52,1656,164],{"class":126},[52,1658,869],{"class":167},[52,1660,887],{"class":126},[52,1662,1663,1665,1667,1669],{"class":54,"line":90},[52,1664,161],{"class":160},[52,1666,164],{"class":126},[52,1668,1167],{"class":167},[52,1670,139],{"class":126},[14,1672,1673],{},"Example:",[43,1675,1677],{"className":45,"code":1676,"language":47,"meta":48,"style":48},"value = 5\n\nprint(value)\nprint(type(value))\n\na, b = value\n",[21,1678,1679,1687,1691,1701,1715,1719],{"__ignoreMap":48},[52,1680,1681,1683,1685],{"class":54,"line":55},[52,1682,59],{"class":58},[52,1684,63],{"class":62},[52,1686,67],{"class":66},[52,1688,1689],{"class":54,"line":70},[52,1690,87],{"emptyLinePlaceholder":86},[52,1692,1693,1695,1697,1699],{"class":54,"line":77},[52,1694,161],{"class":160},[52,1696,164],{"class":126},[52,1698,260],{"class":167},[52,1700,139],{"class":126},[52,1702,1703,1705,1707,1709,1711,1713],{"class":54,"line":83},[52,1704,161],{"class":160},[52,1706,164],{"class":126},[52,1708,880],{"class":393},[52,1710,164],{"class":126},[52,1712,260],{"class":167},[52,1714,887],{"class":126},[52,1716,1717],{"class":54,"line":90},[52,1718,87],{"emptyLinePlaceholder":86},[52,1720,1721,1723,1725,1727,1729],{"class":54,"line":96},[52,1722,27],{"class":58},[52,1724,133],{"class":126},[52,1726,149],{"class":58},[52,1728,63],{"class":62},[52,1730,104],{"class":58},[14,1732,1733],{},"Output before the error:",[43,1735,1737],{"className":45,"code":1736,"language":47,"meta":48,"style":48},"5\n\u003Cclass 'int'>\n",[21,1738,1739,1743],{"__ignoreMap":48},[52,1740,1741],{"class":54,"line":55},[52,1742,457],{"class":66},[52,1744,1745,1747,1749,1751,1753,1755],{"class":54,"line":70},[52,1746,904],{"class":62},[52,1748,907],{"class":661},[52,1750,911],{"class":910},[52,1752,219],{"class":914},[52,1754,917],{"class":910},[52,1756,920],{"class":62},[14,1758,1759,1760,1762],{},"That tells you the problem immediately: ",[21,1761,260],{}," is an integer.",[38,1764,1766],{"id":1765},"common-related-cases","Common related cases",[14,1768,1769],{},"You may see slightly different versions of this error:",[201,1771,1772,1777,1782],{},[204,1773,1774],{},[21,1775,1776],{},"TypeError: cannot unpack non-iterable int object",[204,1778,1779],{},[21,1780,1781],{},"TypeError: cannot unpack non-iterable NoneType object",[204,1783,1784],{},[21,1785,1786],{},"TypeError: cannot unpack non-iterable float object",[14,1788,1789],{},"These all mean the same core thing:",[201,1791,1792,1795],{},[204,1793,1794],{},"Python tried to unpack a value",[204,1796,1797],{},"The value was not iterable",[14,1799,1800],{},"This is different from these errors:",[201,1802,1803,1809],{},[204,1804,1805],{},[27,1806,1808],{"href":1807},"\u002Ferrors\u002Fvalueerror-not-enough-values-to-unpack-fix\u002F","ValueError: not enough values to unpack",[204,1810,1811],{},[27,1812,1814],{"href":1813},"\u002Ferrors\u002Fvalueerror-too-many-values-to-unpack-fix\u002F","ValueError: too many values to unpack",[14,1816,1817,1818,1821],{},"Those errors happen when the value ",[468,1819,1820],{},"is"," iterable, but it has the wrong number of items.",[38,1823,1825],{"id":1824},"faq","FAQ",[518,1827,1829],{"id":1828},"what-does-non-iterable-mean-in-python","What does non-iterable mean in Python?",[14,1831,1832,1833,1835,1836,1835,1838,1840,1841,36],{},"It means the value cannot be looped over or unpacked item by item. Examples include ",[21,1834,219],{},", ",[21,1837,224],{},[21,1839,229],{},", and ",[21,1842,234],{},[518,1844,1846],{"id":1845},"can-i-unpack-a-string-in-python","Can I unpack a string in Python?",[14,1848,1849],{},"Yes. A string is iterable, so you can unpack it if it has the expected number of characters.",[14,1851,1673],{},[43,1853,1855],{"className":45,"code":1854,"language":47,"meta":48,"style":48},"a, b = \"hi\"\nprint(a, b)\n",[21,1856,1857,1875],{"__ignoreMap":48},[52,1858,1859,1861,1863,1865,1867,1869,1872],{"class":54,"line":55},[52,1860,27],{"class":58},[52,1862,133],{"class":126},[52,1864,149],{"class":58},[52,1866,63],{"class":62},[52,1868,1390],{"class":910},[52,1870,1871],{"class":914},"hi",[52,1873,1874],{"class":910},"\"\n",[52,1876,1877,1879,1881,1883,1885,1887],{"class":54,"line":70},[52,1878,161],{"class":160},[52,1880,164],{"class":126},[52,1882,27],{"class":167},[52,1884,133],{"class":126},[52,1886,172],{"class":167},[52,1888,139],{"class":126},[14,1890,177],{},[43,1892,1894],{"className":45,"code":1893,"language":47,"meta":48,"style":48},"h i\n",[21,1895,1896],{"__ignoreMap":48},[52,1897,1898],{"class":54,"line":55},[52,1899,1893],{"class":58},[518,1901,1903],{"id":1902},"why-do-i-get-this-error-from-a-function-call","Why do I get this error from a function call?",[14,1905,1906,1907,1909],{},"Usually because the function returned one value, or returned ",[21,1908,234],{},", but your code expected multiple values.",[518,1911,1913],{"id":1912},"how-is-this-different-from-not-enough-values-to-unpack","How is this different from not enough values to unpack?",[14,1915,1916,1917,1920,1923],{},"This error means the value is not iterable at all.",[1918,1919],"br",{},[21,1921,1922],{},"not enough values to unpack"," means the value is iterable, but it does not contain enough items.",[38,1925,1927],{"id":1926},"see-also","See also",[201,1929,1930,1935,1940,1945,1950,1955],{},[204,1931,1932],{},[27,1933,1934],{"href":743},"Return values in Python functions",[204,1936,1937],{},[27,1938,1939],{"href":298},"Iterators and iterable objects explained",[204,1941,1942],{},[27,1943,1944],{"href":1562},"How to loop through a dictionary in Python",[204,1946,1947],{},[27,1948,1949],{"href":1807},"ValueError: not enough values to unpack (Fix)",[204,1951,1952],{},[27,1953,1954],{"href":1813},"ValueError: too many values to unpack (Fix)",[204,1956,1957],{},[27,1958,1959],{"href":1083},"TypeError: 'NoneType' object is not iterable (Fix)",[1961,1962,1963],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":48,"searchDepth":70,"depth":70,"links":1965},[1966,1967,1968,1969,1970,1971,1976,1981,1987,1988,1989,1995],{"id":40,"depth":70,"text":41},{"id":195,"depth":70,"text":196},{"id":302,"depth":70,"text":303},{"id":353,"depth":70,"text":354},{"id":424,"depth":70,"text":425},{"id":512,"depth":70,"text":513,"children":1972},[1973,1974,1975],{"id":520,"depth":77,"text":521},{"id":586,"depth":77,"text":587},{"id":647,"depth":77,"text":648},{"id":747,"depth":70,"text":748,"children":1977},[1978,1979],{"id":822,"depth":77,"text":823},{"id":926,"depth":77,"text":1980},"Another common case: returning None",{"id":1087,"depth":70,"text":1088,"children":1982},[1983,1984,1985,1986],{"id":1094,"depth":77,"text":1095},{"id":1171,"depth":77,"text":1172},{"id":1248,"depth":77,"text":1249},{"id":1359,"depth":77,"text":1360},{"id":1566,"depth":70,"text":1567},{"id":1765,"depth":70,"text":1766},{"id":1824,"depth":70,"text":1825,"children":1990},[1991,1992,1993,1994],{"id":1828,"depth":77,"text":1829},{"id":1845,"depth":77,"text":1846},{"id":1902,"depth":77,"text":1903},{"id":1912,"depth":77,"text":1913},{"id":1926,"depth":70,"text":1927},"Master typeerror cannot unpack non iterable object fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Ftypeerror-cannot-unpack-non-iterable-object-fix",{"title":5,"description":1996},"errors\u002Ftypeerror-cannot-unpack-non-iterable-object-fix","kYhyri19krqpY1dE7ENEhzrFOqKPlMdmlpgKqaNsy3I",1777585480985]