{"id":75431,"date":"2020-01-23T09:55:12","date_gmt":"2020-01-23T04:25:12","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75431"},"modified":"2020-01-23T09:55:12","modified_gmt":"2020-01-23T04:25:12","slug":"python-zip-function","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/","title":{"rendered":"Learn Python Zip Function with Syntax and Examples"},"content":{"rendered":"<p>In this article, we will focus on the details of the <strong>Python zip() function<\/strong> and see it\u2019s <strong>applications<\/strong>.<\/p>\n<p>We will cover the <strong>syntax<\/strong> of the function, how to<strong> zip() items<\/strong> from <strong>multiple iterables<\/strong> into <strong>one iterable<\/strong> and in the end we will also learn to <strong>unzip<\/strong> the <strong>zip<\/strong>.<\/p>\n<h3>Python Zip() Function<\/h3>\n<p>The <strong>zip()<\/strong> is a <strong>built-in function<\/strong> which means that it is <strong>available<\/strong> to <strong>use anywhere<\/strong> in the <strong>code<\/strong>. It is used to <strong>map similar index<\/strong> <strong>items<\/strong> into a <strong>single entity<\/strong>. It can take <strong>zero<\/strong> or <strong>more arguments<\/strong> and it <strong>returns<\/strong> us an <strong>iterator<\/strong> to the <strong>tuple<\/strong>.<\/p>\n<p><strong>Syntax<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">zip(*iterables)<\/pre>\n<h3>Python Zip() Parameters<\/h3>\n<p><strong>Python zip function<\/strong> takes<strong> built-in<\/strong> or <strong>user-defined iterables<\/strong> like <strong>lists<\/strong>, <strong>strings<\/strong>, <strong>dictionary<\/strong>, etc.<\/p>\n<p>Let\u2019s see this with an example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( zip() )\nprint( zip([1,2,3], [1,2,4] ))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&lt;zip object at 0x000002C61C827188&gt;<br \/>\n&lt;zip object at 0x000002C61C827188&gt;<\/div>\n<p>We can see that the function <strong>returns<\/strong> us an <strong>iterator<\/strong> to a <strong>tuple object<\/strong>. So we can use a <strong>for loop<\/strong> to <strong>iterate<\/strong> over all the <strong>items<\/strong> which have been <strong>zipped<\/strong>.<\/p>\n<h3>Python Zip() Function Examples<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-zip-function-examples.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75584 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-zip-function-examples.jpg\" alt=\"python zip function examples\" width=\"610\" height=\"456\" \/><\/a><\/h3>\n<h4>1. No Arguments<\/h4>\n<p>When <strong>no arguments<\/strong> are <strong>passed<\/strong> to the<strong> Python zip() function<\/strong> then the <strong>zip() returns<\/strong> an <strong>iterator<\/strong> to an <strong>empty tuple<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(list(zip()))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[]<\/div>\n<p>As you can see we got an <strong>empty list<\/strong> when we <strong>converted<\/strong> the <strong>iterator<\/strong> into a <strong>list<\/strong>.<\/p>\n<h4>2. Single Argument<\/h4>\n<p>When a <strong>single iterable argument<\/strong> is <strong>passed<\/strong> to the <strong>zip() function<\/strong>, the result is <strong>zipped<\/strong> in an <strong>iterator<\/strong>. We will use a <strong>for loop<\/strong> to see all the <strong>items<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for i in zip([1,2,3,4,5]):\n  print(i)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(1,)<br \/>\n(2,)<br \/>\n(3,)<br \/>\n(4,)<br \/>\n(5,)<\/div>\n<p>Each item of the <strong>iterable<\/strong> <strong>argument<\/strong> is <strong>converted<\/strong> into a <strong>tuple<\/strong>.<\/p>\n<h4>3. Multiple Arguments of Same Length<\/h4>\n<p>The main purpose of <strong>Python zip()<\/strong> function is seen when we pass <strong>multiple arguments<\/strong> of the <strong>same length<\/strong>. As a result, we will get an <strong>iterator<\/strong> to the <strong>items<\/strong> of the <strong>same index mapped<\/strong> in a <strong>tuple<\/strong>.<\/p>\n<p>You will clearly understand this by looking at the example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">student = [\u2018Shrangi\u2019, \u2018Akshay\u2019, \u2018Aman\u2019 , \u2018Joy\u2019]\nbook_id = [1969, 2453, 2189, 1865 ]\nprice = [1100, 780, 800, 950]\n\nfor record in zip(student, book_id, price):\n  print(record)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(\u2018Shrangi\u2019, 1969, 1100)<br \/>\n(\u2018Akshay\u2019, 2453, 780)<br \/>\n(\u2018Aman\u2019, 2189, 800)<br \/>\n(\u2018Joy\u2019, 1865, 950)<\/div>\n<h4>4. Multiple Arguments of Different Length<\/h4>\n<p>We saw <strong>multiple arguments<\/strong> of the <strong>same length<\/strong> but what will happen to the <strong>index<\/strong> when <strong>different length arguments<\/strong> are <strong>passed<\/strong>.<\/p>\n<p>Let\u2019s see a quick example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for i in zip([1,2,3,4],[1,2,3],[1,2,3,4,5]):\n  print(i)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(1, 1, 1)<br \/>\n(2, 2, 2)<br \/>\n(3, 3, 3)<\/div>\n<p>Here we see that the<strong> zip() function<\/strong> will only <strong>map items<\/strong> from <strong>left to right.<\/strong><\/p>\n<p>If one of the <strong>iterable<\/strong> in <strong>arguments<\/strong> is <strong>exhausted <\/strong>then it <strong>doesn\u2019t yield<\/strong> further.<\/p>\n<h3>Unzipping Values in Python<\/h3>\n<p>Till now we discussed <strong>zipping<\/strong> the <strong>values together<\/strong>. Now, let\u2019s see how we can <strong>unzip<\/strong> them.<\/p>\n<p>For <strong>unzipping values<\/strong> we use the <strong>* operator<\/strong> it is similar to the <strong>tuples unpacking<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">name = [\u201cAmanda\u201d, \u201cJoey\u201d, \u201cSheldon\u201d, \u201cRebecca\u201d]\nid = [1,2,3,4]\n\n#zipping values\nrecord = list( zip(name, id) )\nprint(record)\n\n#unzipping values\nnames, ids = zip(*record)\nprint(names)\nprint(ids)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[(&#8216;Amanda&#8217;, 1), (&#8216;Joey&#8217;, 2), (&#8216;Sheldon&#8217;, 3), (&#8216;Rebecca&#8217;, 4)]<br \/>\n(&#8216;Amanda&#8217;, &#8216;Joey&#8217;, &#8216;Sheldon&#8217;, &#8216;Rebecca&#8217;)<br \/>\n(1, 2, 3, 4)<\/div>\n<h3>Summary<\/h3>\n<p>In this Python zip function tutorial, we discussed the <strong>inbuilt zip()<\/strong> function of Python. We learnt how we can <strong>map similar<\/strong> <strong>indexes<\/strong> items of <strong>different iterables<\/strong> into a <strong>single entity<\/strong>.<\/p>\n<p>We first saw the syntax of the <strong>function<\/strong> and then saw the <strong>behaviour<\/strong> of the function with a <strong>different number of arguments<\/strong>. At last, we learned how to <strong>unzip<\/strong> the <strong>zipped values<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will focus on the details of the Python zip() function and see it\u2019s applications. We will cover the syntax of the function, how to zip() items from multiple iterables into&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75585,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1368,1369,1370,1371,1372,1373,1374,1375],"class_list":["post-75431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-unzip","tag-python-zip","tag-python-zip-function","tag-python-zip-function-example","tag-python-zip-parameters","tag-unzipping-values-in-python","tag-zip-function-in-python","tag-zip-function-in-python-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Learn Python Zip Function with Syntax and Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python zip() function is a built-in function which means, that it is available to use anywhere in the code. Check zip function examples, parameters, etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn Python Zip Function with Syntax and Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python zip() function is a built-in function which means, that it is available to use anywhere in the code. Check zip function examples, parameters, etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-23T04:25:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-zip-function.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn Python Zip Function with Syntax and Examples - TechVidvan","description":"Python zip() function is a built-in function which means, that it is available to use anywhere in the code. Check zip function examples, parameters, etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/","og_locale":"en_US","og_type":"article","og_title":"Learn Python Zip Function with Syntax and Examples - TechVidvan","og_description":"Python zip() function is a built-in function which means, that it is available to use anywhere in the code. Check zip function examples, parameters, etc.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-23T04:25:12+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-zip-function.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Learn Python Zip Function with Syntax and Examples","datePublished":"2020-01-23T04:25:12+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/"},"wordCount":498,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-zip-function.jpg","keywords":["python unzip","Python zip","Python Zip function","Python Zip Function example","Python Zip() Parameters","Unzipping Values in Python","Zip Function in Python","zip function in python example"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/","url":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/","name":"Learn Python Zip Function with Syntax and Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-zip-function.jpg","datePublished":"2020-01-23T04:25:12+00:00","description":"Python zip() function is a built-in function which means, that it is available to use anywhere in the code. Check zip function examples, parameters, etc.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-zip-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-zip-function.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-zip-function.jpg","width":802,"height":420,"caption":"zip function in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-zip-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Learn Python Zip Function with Syntax and Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75431"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75585"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}