{"id":328,"date":"2020-10-07T02:39:08","date_gmt":"2020-10-07T02:39:08","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=328"},"modified":"2025-03-26T13:52:36","modified_gmt":"2025-03-26T13:52:36","slug":"python-for-loop-list","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-for-loop-list\/","title":{"rendered":"How to Use a For Loop to Iterate over a List"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python <code>for<\/code> loop to iterate over a list in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-python-for-loop-to-iterate-over-a-list'>Using Python for loop to iterate over a list <a href=\"#using-python-for-loop-to-iterate-over-a-list\" class=\"anchor\" id=\"using-python-for-loop-to-iterate-over-a-list\" title=\"Anchor for Using Python for loop to iterate over a list\">#<\/a><\/h2>\n\n\n\n<p>To iterate over a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a>, you use the <code>for<\/code> loop statement as follows:<\/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\"><span class=\"hljs-keyword\">for<\/span> item <span class=\"hljs-keyword\">in<\/span> list:\n    <span class=\"hljs-comment\"># process the item<\/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>In this syntax, the <code>for<\/code> loop statement assigns an individual element of the <code>list<\/code> to the <code>item<\/code> variable in each iteration. <\/p>\n\n\n\n<p>Inside the body of the loop, you can manipulate each list element individually. <\/p>\n\n\n\n<p>For example, the following defines a list of cities and uses a <code>for<\/code> loop to iterate over the list:<\/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\">cities = &#91;<span class=\"hljs-string\">'New York'<\/span>, <span class=\"hljs-string\">'Beijing'<\/span>, <span class=\"hljs-string\">'Cairo'<\/span>, <span class=\"hljs-string\">'Mumbai'<\/span>, <span class=\"hljs-string\">'Mexico'<\/span>]\n\n<span class=\"hljs-keyword\">for<\/span> city <span class=\"hljs-keyword\">in<\/span> cities:\n    print(city)<\/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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2l0aWVzID0gWydOZXcgWW9yaycsICdCZWlqaW5nJywgJ0NhaXJvJywgJ011bWJhaScsICdNZXhpY28nXQoKZm9yIGNpdHkgaW4gY2l0aWVzOgogICAgcHJpbnQoY2l0eSk%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-3\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">New York\nBeijing\nCairo\nMumbai\nMexico<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>for<\/code> loop assigns an individual element of the <code>cities<\/code> list to the <code>city<\/code> variable and prints out the <code>city<\/code> in each iteration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-python-for-loop-to-iterate-over-a-list-with-index'>Using Python for loop to iterate over a list with index <a href=\"#using-python-for-loop-to-iterate-over-a-list-with-index\" class=\"anchor\" id=\"using-python-for-loop-to-iterate-over-a-list-with-index\" title=\"Anchor for Using Python for loop to iterate over a list with index\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you may want to access indexes of elements inside the loop. In these cases, you can use the <code>enumerate()<\/code> function.<\/p>\n\n\n\n<p>The <code>enumerate()<\/code> function returns a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-tuples\/\">tuple<\/a> that contains the current index and element of the list.<\/p>\n\n\n\n<p>The following example defines a list of cities and uses a <code>for<\/code> loop with the <code>enumerate()<\/code> function to iterate over the list:<\/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\">cities = &#91;<span class=\"hljs-string\">'New York'<\/span>, <span class=\"hljs-string\">'Beijing'<\/span>, <span class=\"hljs-string\">'Cairo'<\/span>, <span class=\"hljs-string\">'Mumbai'<\/span>, <span class=\"hljs-string\">'Mexico'<\/span>]\n\n<span class=\"hljs-keyword\">for<\/span> item <span class=\"hljs-keyword\">in<\/span> enumerate(cities):\n    print(item)<\/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=Y2l0aWVzID0gWydOZXcgWW9yaycsICdCZWlqaW5nJywgJ0NhaXJvJywgJ011bWJhaScsICdNZXhpY28nXQoKZm9yIGl0ZW0gaW4gZW51bWVyYXRlKGNpdGllcyk6CiAgICBwcmludChpdGVtKQ%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-5\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">(0, 'New York')\n(1, 'Beijing')\n(2, 'Cairo')\n(3, 'Mumbai')\n(4, 'Mexico')<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To access the index, you can <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-unpack-list\/\">unpack the tuple<\/a> within the <code>for<\/code> loop statement like this:<\/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\">cities = &#91;<span class=\"hljs-string\">'New York'<\/span>, <span class=\"hljs-string\">'Beijing'<\/span>, <span class=\"hljs-string\">'Cairo'<\/span>, <span class=\"hljs-string\">'Mumbai'<\/span>, <span class=\"hljs-string\">'Mexico'<\/span>]\n\n<span class=\"hljs-keyword\">for<\/span> index, city <span class=\"hljs-keyword\">in<\/span> enumerate(cities):\n    print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{index}<\/span>: <span class=\"hljs-subst\">{city}<\/span>\"<\/span>)<\/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=Y2l0aWVzID0gWydOZXcgWW9yaycsICdCZWlqaW5nJywgJ0NhaXJvJywgJ011bWJhaScsICdNZXhpY28nXQoKZm9yIGluZGV4LCBjaXR5IGluIGVudW1lcmF0ZShjaXRpZXMpOgogICAgcHJpbnQoZiJ7aW5kZXh9OiB7Y2l0eX0iKQ%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=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">0: New York\n1: Beijing\n2: Cairo\n3: Mumbai\n4: Mexico<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>enumerate()<\/code> function allows you to specify the starting index which defaults to zero.<\/p>\n\n\n\n<p>The following example uses the <code>enumerate()<\/code> function with the index that starts from one:<\/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\">cities = &#91;<span class=\"hljs-string\">'New York'<\/span>, <span class=\"hljs-string\">'Beijing'<\/span>, <span class=\"hljs-string\">'Cairo'<\/span>, <span class=\"hljs-string\">'Mumbai'<\/span>, <span class=\"hljs-string\">'Mexico'<\/span>]\n\n<span class=\"hljs-keyword\">for<\/span> index, city <span class=\"hljs-keyword\">in<\/span> enumerate(cities,<span class=\"hljs-number\">1<\/span>):\n    print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{index}<\/span>: <span class=\"hljs-subst\">{city}<\/span>\"<\/span>)<\/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=Y2l0aWVzID0gWydOZXcgWW9yaycsICdCZWlqaW5nJywgJ0NhaXJvJywgJ011bWJhaScsICdNZXhpY28nXQoKZm9yIGluZGV4LCBjaXR5IGluIGVudW1lcmF0ZShjaXRpZXMsMSk6CiAgICBwcmludChmIntpbmRleH06IHtjaXR5fSIp\" 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=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">1: New York\n2: Beijing\n3: Cairo\n4: Mumbai\n5: Mexico<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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>Use a <code>for<\/code> loop to iterate over a list.<\/li>\n\n\n\n<li>Use a <code>for<\/code> loop with the <code>enumerate()<\/code> function to access indexes.<\/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-for-loop\"\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=\"328\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-for-loop-list\/\"\n\t\t\t\tdata-post-title=\"How to Use a For Loop to Iterate over a List\"\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=\"328\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-for-loop-list\/\"\n\t\t\t\tdata-post-title=\"How to Use a For Loop to Iterate over a List\"\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 use the Python for loop to iterate over a list in Python.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":31,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-328","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/328","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=328"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/328\/revisions"}],"predecessor-version":[{"id":7021,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/328\/revisions\/7021"}],"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=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}