{"id":320,"date":"2020-10-07T01:20:06","date_gmt":"2020-10-07T01:20:06","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=320"},"modified":"2025-03-26T13:51:32","modified_gmt":"2025-03-26T13:51:32","slug":"python-unpack-list","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-unpack-list\/","title":{"rendered":"How to Unpack a List in Python"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to unpack a list in Python to make your code more concise.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-list-unpacking'>Introduction to the list unpacking <a href=\"#introduction-to-the-list-unpacking\" class=\"anchor\" id=\"introduction-to-the-list-unpacking\" title=\"Anchor for Introduction to the list unpacking\">#<\/a><\/h2>\n\n\n\n<p>The following example defines a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> of <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string\/\">strings<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'green'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To assign the first, second, and third elements of the list to <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/\">variables<\/a>, you may assign individual elements to variables like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'green'<\/span>]\n\nred = colors&#91;<span class=\"hljs-number\">0<\/span>]\nblue = colors&#91;<span class=\"hljs-number\">1<\/span>]\ngreen = colors&#91;<span class=\"hljs-number\">2<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, Python provides a better way to do this. It&#8217;s called sequence unpacking.<\/p>\n\n\n\n<p>Basically, you can assign elements of a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> (and also a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-tuples\/\">tuple<\/a>) to multiple variables. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'green'<\/span>]\n\nred, blue, green = colors\n\nprint(red)\nprint(green)\nprint(blue)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnYmx1ZScsICdncmVlbiddCgpyZWQsIGJsdWUsIGdyZWVuID0gY29sb3JzCgpwcmludChyZWQpCnByaW50KGdyZWVuKQpwcmludChibHVlKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>This statement assigns the first, second, and third elements of the <code>colors<\/code> list to the <code>red<\/code>, <code>blue<\/code>, and <code>green<\/code> variables.<\/p>\n\n\n\n<p>In this example, the number of variables on the left side is the same as the number of elements in the list on the right side.<\/p>\n\n\n\n<p>If you use a fewer number of variables on the left side, you&#8217;ll get an error. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'green'<\/span>]\nred, blue = colors\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnYmx1ZScsICdncmVlbiddCnJlZCwgYmx1ZSA9IGNvbG9ycw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">ValueError: too many values to unpack (expected <span class=\"hljs-number\">2<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case, Python could not unpack three elements to two variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='unpacking-and-packing'>Unpacking and packing <a href=\"#unpacking-and-packing\" class=\"anchor\" id=\"unpacking-and-packing\" title=\"Anchor for Unpacking and packing\">#<\/a><\/h2>\n\n\n\n<p>If you want to unpack the first few elements of a list and don&#8217;t care about the other elements, you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, unpack the needed elements to variables.<\/li>\n\n\n\n<li>Second, pack the leftover elements into a new list and assign it to another variable.<\/li>\n<\/ul>\n\n\n\n<p>By putting the asterisk (<code>*<\/code>) in front of a variable name, you&#8217;ll pack the leftover elements into a list and assign them to a variable. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'green'<\/span>]\nred, blue, *other = colors\n\nprint(red)\nprint(blue)\nprint(other)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnYmx1ZScsICdncmVlbiddCnJlZCwgYmx1ZSwgKm90aGVyID0gY29sb3JzCgpwcmludChyZWQpCnByaW50KGJsdWUpCnByaW50KG90aGVyKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">red\nblue\n&#91;<span class=\"hljs-string\">'green'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example assigns the first and second elements of the <code>colors<\/code> list to the <code>red<\/code> and <code>green<\/code> variables. And it assigns the last element of the list to the <code>other<\/code> variable.<\/p>\n\n\n\n<p>Here&#8217;s another example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'cyan'<\/span>, <span class=\"hljs-string\">'magenta'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'black'<\/span>]\ncyan, magenta, *other = colors\n\nprint(cyan)\nprint(magenta)\nprint(other)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydjeWFuJywgJ21hZ2VudGEnLCAneWVsbG93JywgJ2JsYWNrJ10KY3lhbiwgbWFnZW50YSwgKm90aGVyID0gY29sb3JzCgpwcmludChjeWFuKQpwcmludChtYWdlbnRhKQpwcmludChvdGhlcik%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">cyan\nmagenta\n&#91;<span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'black'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example assigns the first and second elements to variables. It packs the last two elements in a new list and assigns the new list to the <code>other<\/code> variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Unpacking assigns elements of the list to multiple variables.<\/li>\n\n\n\n<li>Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=list-unpack\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"320\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-unpack-list\/\"\n\t\t\t\tdata-post-title=\"How to Unpack a List in Python\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"320\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-unpack-list\/\"\n\t\t\t\tdata-post-title=\"How to Unpack a List in Python\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to unpack a list in Python to make your code more concise.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-320","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=320"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/320\/revisions"}],"predecessor-version":[{"id":7020,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/320\/revisions\/7020"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}