{"id":5224,"date":"2019-12-07T09:15:12","date_gmt":"2019-12-07T03:45:12","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5224"},"modified":"2019-12-08T00:09:02","modified_gmt":"2019-12-07T18:39:02","slug":"how-to-pass-two-or-more-lists-in-for-loop-using-python","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-pass-two-or-more-lists-in-for-loop-using-python\/","title":{"rendered":"HOW TO PASS TWO OR MORE LISTS IN FOR LOOP USING PYTHON"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">HOW TO PASS TWO OR MORE LISTS IN FOR LOOP USING PYTHON<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Date: 07\/12\/2019<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Introduction<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python has global built-ins that are handy for a regular problem. Specifically, I will explain <code>zip()<\/code> in this article.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s take an example from Avengers, <br>  <br>      Have to match the list of  Avengers characters with the persons who acted in the role. For example, We will pick up some characters such as Iron Man, Captain America, Thor and Groot (newly added avenger but still he is a Guardian).<\/p>\n\n\n\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=\"\">avengers_characters=[\"Iron Man\",\"Captain America\",\"Thor\",\"Groot\"]\navengers_personalities=[\"Robert Downey JR\",\"Chris Evans\",\"Chris Hemsworth\",\"Vin diesel\"]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">     Okay, now I want to print the matching characters with the persons who acted in the roles.<br> <br>     For that we have to use for loop right, Yes here it will be like<\/p>\n\n\n\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 characters,personalities in zip (avengers_characters,avengers_personalities) :\n    print(\"{} by {}\".format(characters,personalities))   <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">    Wait a minute. Don&#8217;t get confused about what is zip and why I used it. I will explain it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>    By for loop we can iterate only one list but by using <strong>zip<\/strong> function we can iterate multiple lists parallelly,  where the first item in each passed iterator is paired together and then the second item in each passed iterator are paired together etc.. and even single list also be passed and results in a list of tuples. the <strong>format function is used to position formatting by the order of parentheses<\/strong> <strong>{}<\/strong> and value passing.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax for zip:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><br>         <strong>   zip(<em>iterator1, iterator2, iterator3 &#8230;<\/em>) <\/strong>   <br><br>Here is the output:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"284\" height=\"90\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/Screenshot-from-2019-12-05-17-20-53-1.png\" alt=\"\" class=\"wp-image-5230\"\/><\/figure><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> Note:<\/p><p>      In zip function , If passed iterators have different lengths, the iterator with the least items decides the length of the new iterator. ie If one list have 2 values and other lists have  3 values then the for loop  will  end  in the length of 2 and other one value will be discarded.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the example for mismatching length of two lists and its Output:<\/p>\n\n\n\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=\"\">avengers_characters=[\"Iron Man\",\"Captain America\",\"Thor\",\"Groot\"]\navengers_personalities=[\"Robert Downey JR\",\"Chris Evans\",\"Chris Hemsworth\"]\n\nfor characters,personalities in zip(avengers_characters,avengers_personalities) :\n    print(\"{} by {}\".format(characters,personalities))<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"292\" height=\"75\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/Screenshot-from-2019-12-06-09-27-51.png\" alt=\"\" class=\"wp-image-5252\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">To overcome this, there is a package in python called <strong>itertools<\/strong> in its there is function called <strong>izip_longest<\/strong> to perform up to the maximum length of the iterator .ie If one list has 2 values and other lists have 3 values then the for loop will execute up to the length of 3  and for missing value in a list will be filled by a default value called <strong>none<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the example of mismatching length of lists by using <strong>itertools<\/strong> and its output:<\/p>\n\n\n\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=\"\">#some of the characters\navengers_characters=[\"Iron Man\",\"Captain America\",\"Thor\",\"Groot\"]\navengers_personalities=[\"Robert Downey JR\",\"Chris Evans\",\"Chris Hemsworth\"]\n\n\n#matching the avengers characters with the personalities who acted\nfor characters,personalities in itertools.izip_longest(avengers_characters,avengers_personalities) :\n    print(\"{} by {}\".format(characters,personalities))\n   <\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"268\" height=\"89\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/Screenshot-from-2019-12-06-09-34-50-1.png\" alt=\"\" class=\"wp-image-5257\"\/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You for using <a href=\"https:\/\/pheonixsolutions.com\/\">pheonixsolutions<\/a>. <br>If you like the blog share it with your friends and others also.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HOW TO PASS TWO OR MORE LISTS IN FOR LOOP USING PYTHON Date: 07\/12\/2019 Introduction Python has global built-ins that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[600,292],"class_list":["post-5224","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-list","tag-python","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1mg","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5224"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5224\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}