{"id":126863,"date":"2022-07-31T17:38:45","date_gmt":"2022-07-31T17:38:45","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=528311"},"modified":"2022-07-31T17:38:45","modified_gmt":"2022-07-31T17:38:45","slug":"for-loop-with-two-variables-for-i-j-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/31\/for-loop-with-two-variables-for-i-j-in-python\/","title":{"rendered":"For Loop with Two Variables (for i j in python)"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;528311&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2>for i j in python<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The Python expression <code>for i, j in XXX<\/code> allows you to iterate over an <a href=\"https:\/\/blog.finxter.com\/iterators-iterables-and-itertools\/\" data-type=\"post\" data-id=\"29507\" target=\"_blank\" rel=\"noreferrer noopener\">iterable<\/a> <code>XXX<\/code> of pairs of values. For example, to iterate over a list of tuples, this expression captures both <a href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"post\" data-id=\"12043\" target=\"_blank\" rel=\"noreferrer noopener\">tuple<\/a> values in the loop variables <code>i<\/code> and <code>j<\/code> at once in each iteration. <\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i, j in [('Alice', 18), ('Bob', 22)]: print(i, 'is', j, 'years old')<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Alice is 18 years old\nBob is 22 years old<\/pre>\n<p>Notice how the first loop iteration captures <code>i='Alice'<\/code> and <code>j=18<\/code>, whereas the second loop iteration captures <code>i='Bob'<\/code> and <code>j=22<\/code>. <\/p>\n<h2>for i j in enumerate python<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The Python expression <code>for i, j in enumerate(iterable)<\/code> allows you to loop over an iterable using variable <code>i<\/code> as a counter (0, 1, 2, &#8230;) and variable <code>j<\/code> to capture the iterable elements.<\/p>\n<p>Here&#8217;s an example where we assign the counter values <code>i=0,1,2<\/code> to the three elements in <code>lst<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lst = ['Alice', 'Bob', 'Carl']\nfor i, j in enumerate(lst): print(i, j)\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">0 Alice\n1 Bob\n2 Carl<\/pre>\n<p>Notice how the loop iteration capture:<\/p>\n<ul>\n<li><code>i=0<\/code> and <code>j='Alice'<\/code>,<\/li>\n<li><code>i=1<\/code> and <code>j='Bob'<\/code>, and<\/li>\n<li><code>i=2<\/code> and <code>j='Carl'<\/code>.<\/li>\n<\/ul>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Learn More<\/strong>: The <code><a href=\"https:\/\/blog.finxter.com\/python-enumerate\/\" data-type=\"post\" data-id=\"20466\" target=\"_blank\" rel=\"noreferrer noopener\">enumerate()<\/a><\/code> function in Python.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/blog.finxter.com\/python-enumerate\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-117.png\" alt=\"Python enumerate()\" class=\"wp-image-528379\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-117.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-117-300x169.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-117-768x432.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<h2>for i j in zip python<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The Python expression <code>for i, j in zip(iter_1, iter_2)<\/code> allows you to align the values of two iterables <code>iter_1<\/code> and <code>iter_2<\/code> in an ordered manner and iterate over the pairs of elements. We capture the two elements at the same positions in variables <code>i<\/code> and <code>j<\/code>.<\/p>\n<p>Here&#8217;s an example that zips together the two <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\">li<\/a><a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\" target=\"_blank\" rel=\"noreferrer noopener\">s<\/a><a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\">ts<\/a> <code>[1,2,3]<\/code> and <code>[9,8,7,6,5]<\/code>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\nfor i, j in zip([1,2,3], [9,8,7,6,5]): print(i, j)\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1 9\n2 8\n3 7<\/pre>\n<\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Learn More<\/strong>: The <code><a href=\"https:\/\/blog.finxter.com\/python-ziiiiiiip-a-helpful-guide\/\" data-type=\"post\" data-id=\"1938\" target=\"_blank\" rel=\"noreferrer noopener\">zip()<\/a><\/code> function in Python.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/blog.finxter.com\/python-ziiiiiiip-a-helpful-guide\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-118.png\" alt=\"\" class=\"wp-image-528457\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-118.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-118-300x169.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-118-768x432.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<h2>for i j in list python<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The Python expression <code>for i, j in list<\/code> allows you to iterate over a given list of pairs of elements (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-merge-lists-into-a-list-of-tuples\/\" data-type=\"post\" data-id=\"9506\" target=\"_blank\">list of tuples<\/a> or <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" data-type=\"post\" data-id=\"7890\" target=\"_blank\">list of lists<\/a>). In each iteration, this expression captures both pairs of elements at once in the loop variables <code>i<\/code> and <code>j<\/code>. <\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i, j in [(1,9), (2,8), (3,7)]: print(i, j)<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1 9\n2 8\n3 7<\/pre>\n<h2>for i j k python<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The Python expression <code>for i, j, k in iterable<\/code> allows you to iterate over a given list of triplets of elements (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-merge-lists-into-a-list-of-tuples\/\" data-type=\"post\" data-id=\"9506\" target=\"_blank\">list of tuples<\/a> or <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" data-type=\"post\" data-id=\"7890\" target=\"_blank\">list of lists<\/a>). In each iteration, this expression captures all three elements at once in the loop variables <code>i<\/code> and <code>j<\/code>. <\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i, j, k in [(1,2,3), (4,5,6), (7,8,9)]: print(i, j, k)<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1 2 3\n4 5 6\n7 8 9<\/pre>\n<h2>for i j in a b python<\/h2>\n<p>Given two lists <code>a<\/code> and <code>b<\/code>, you can iterate over both lists at once by using the expression <code>for i,j in zip(a,b)<\/code>. <\/p>\n<p>Given one list <code>['a', 'b']<\/code>, you can use the expression <code>for i,j in enumerate(['a', 'b'])<\/code> to iterate over the pairs of (identifier, list element) <a href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"post\" data-id=\"12043\" target=\"_blank\" rel=\"noreferrer noopener\">tuples<\/a>. <\/p>\n<h2>Summary<\/h2>\n<p>The Python <code>for<\/code> loop is a powerful method to iterate over multiple iterables at once, usually with the help of the <code>zip()<\/code> or <code>enumerate()<\/code> functions.<\/p>\n<pre class=\"wp-block-preformatted\"><code>for i, j in zip(range(10), range(10)): # (0,0), (1,1), ..., (9,9)<\/code><\/pre>\n<p>If a list element is an iterable by itself, you can capture all iterable elements using a comma-separated list when defining the loop variables.<\/p>\n<pre class=\"wp-block-preformatted\"><code>for i,j,k in [(1,2,3), (4,5,6)]: # Do Something<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) for i j in python The Python expression for i, j in XXX allows you to iterate over an iterable XXX of pairs of values. For example, to iterate over a list of tuples, this expression captures both tuple values in the loop variables i and j at once in each [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-126863","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126863"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126863\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}