

{"id":25863,"date":"2018-08-24T05:16:24","date_gmt":"2018-08-23T23:46:24","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=25863"},"modified":"2026-04-20T14:40:10","modified_gmt":"2026-04-20T09:10:10","slug":"python-zip-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-zip-function\/","title":{"rendered":"Python Zip Function With Examples | Python Unzipping values"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In this Python tutorial, we will discuss the Python Zip Function with an example. Also, we will understand Zip in Python and Python Unzipping Values.<\/span><\/p>\n<p>So, let&#8217;s start the Python Zip Function tutorial.<\/p>\n<h3>What is the Python Zip Function?<\/h3>\n<p><span style=\"font-weight: 400\">Like we\u2019ve said manifold times before, the interpreter for Python has some types and functions built into it; these are the ones always available to it. <\/span><\/p>\n<p><span style=\"font-weight: 400\">zip() is one such function, and we saw a brief on it when we talked about <\/span>Built-in Functions<span style=\"font-weight: 400\">. Let\u2019s take a quick recap before we can proceed to explain this to you from scratch.<\/span><\/p>\n<p><span style=\"font-weight: 400\">zip() ties two or more sequences side by side, making pairs (or tuples) of matching positions. Picture a zipper joining the teeth of two rails\u2014that is exactly how zip bonds lists, tuples, or even strings.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Let&#8217;s take a quick example of the Zip Function in Python<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in zip([1,2,3],['a','b','c']):\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, &#8216;a&#8217;)<br \/>\n(2, &#8216;b&#8217;)<br \/>\n(3, &#8216;c&#8217;)<\/div>\n<h3>Use of Zip Function in Python<\/h3>\n<ul>\n<li><strong>Loop together:<\/strong> It lets you go through many lists at the same time without using an index.<\/li>\n<li><strong>Wrapping rows and columns:<\/strong> It can be used to change the rows into columns in a table.<\/li>\n<li><strong>Making dictionaries:<\/strong> You can easily create a dictionary by combining one of the keys and the values.<\/li>\n<\/ul>\n<h3>Understanding Python zip()<\/h3>\n<p><span style=\"font-weight: 400\">Like Ziploc in the real world and .zip files in the virtual one, a zip is a kind of container. Like a .zip file holds real files within itself, a zip holds real data within. <\/span><\/p>\n<p><span style=\"font-weight: 400\">It takes iterable elements as input and returns an iterator on them (an iterator of tuples). It evaluates the iterables left to right.<\/span><\/p>\n<h4>1. The syntax for Python Zip Function<\/h4>\n<p><span style=\"font-weight: 400\">Python zip() function has the following syntax-<\/span><\/p>\n<p><strong>zip(*iterables)<\/strong><\/p>\n<p><span style=\"font-weight: 400\">As arguments, it can take iterables, we see. These can be built-in, like the list, string, dict, and user-defined (objects with the __iter__ method).<\/span><\/p>\n<h4>2. Python Zip Function Example<\/h4>\n<ul>\n<li style=\"font-weight: 400\"><strong>No arguments<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">What happens when we provide no arguments to zip()?<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; set(zip())<\/pre>\n<p>set()<br \/>\n<span style=\"font-weight: 400\">You can see that this returns an empty iterator.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><strong>Single argument<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in zip([1,2,3]):\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1,)<br \/>\n(2,)<br \/>\n(3,)<\/div>\n<p><span style=\"font-weight: 400\">This returns tuples holding single values<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><strong>Multiple arguments of the same length<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">So, let\u2019s pass these two lists of equal length.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in zip([1,2,3],['a','b','c']):\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, &#8216;a&#8217;)<br \/>\n(2, &#8216;b&#8217;)<br \/>\n(3, &#8216;c&#8217;)<\/div>\n<p><span style=\"font-weight: 400\">This zips elements together from each list. How about more than two?<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in zip([1,2,3],['a','b','c'],['#','*','$']):\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, &#8216;a&#8217;, &#8216;#&#8217;)<br \/>\n(2, &#8216;b&#8217;, &#8216;*&#8217;)<br \/>\n(3, &#8216;c&#8217;, &#8216;$&#8217;)<\/div>\n<ul>\n<li style=\"font-weight: 400\"><strong>Multiple arguments of different lengths<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">When we provide multiple lists of different lengths, it stops at the shortest one.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; set(zip([1,2],[3,4,5]))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{(1, 3), (2, 4)}<\/div>\n<p><span style=\"font-weight: 400\">If you want to keep those, you can borrow zip_longest() from itertools.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import zip_longest as zl\r\n&gt;&gt;&gt; set(zl([1,2],[3,4,5]))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{(1, 3), (2, 4), (None, 5)}<\/div>\n<h3>Unzipping Values in Python<\/h3>\n<p><span style=\"font-weight: 400\">Now we know how to zip values together. But how to unzip them? Well, we use the * character with the zip() function.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; z=zip([1,2,3],['a','b','c'],['#','*','$'])\r\n&gt;&gt;&gt; a,b,c=zip(*z)\r\n&gt;&gt;&gt; a,b,c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">((1, 2, 3), (&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;), (&#8216;#&#8217;, &#8216;*&#8217;, &#8216;$&#8217;))<\/div>\n<p><span style=\"font-weight: 400\">So, this unzips the zip object <\/span><i><span style=\"font-weight: 400\">z<\/span><\/i><span style=\"font-weight: 400\"> into the variables <\/span><i><span style=\"font-weight: 400\">a<\/span><\/i><span style=\"font-weight: 400\">, <\/span><i><span style=\"font-weight: 400\">b<\/span><\/i><span style=\"font-weight: 400\">, and <\/span><i><span style=\"font-weight: 400\">c<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; z=zip([1,2],[3,4,5])\r\n&gt;&gt;&gt; a,b=zip(*z)\r\n&gt;&gt;&gt; a,b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">((1, 2), (3, 4))<\/div>\n<p><span style=\"font-weight: 400\">Now, notice that this dropped the element 5 because it didn\u2019t zip into anything anyway.<\/span><\/p>\n<h3>Python Interview Questions on Zip Function<\/h3>\n<p>1. What is the Python zip Function? Explain with an example.<\/p>\n<p>2. What does zip() do in Python?<\/p>\n<p>3. How to get a zip file in Python?<\/p>\n<p>4. How do you zip two lists in Python?<\/p>\n<p>5. Can Python read zip files?<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, in this tutorial, we discussed Python Zip Functions in detail with examples. Moreover, we saw unzipping values in Python.<\/p>\n<p>If you have any queries, ask in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python tutorial, we will discuss the Python Zip Function with an example. Also, we will understand Zip in Python and Python Unzipping Values. So, let&#8217;s start the Python Zip Function tutorial. What&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":25901,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[9176,10934,10935,10936,10937,10938,10939,14038,16347,16348],"class_list":["post-25863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-numpy-zip-function","tag-python-zip","tag-python-zip-for-loop","tag-python-zip-function","tag-python-zip-function-example","tag-python-zip-list-of-lists","tag-python-zip-object","tag-syntax-for-python-zip-function","tag-zip-function-in-python","tag-zip-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Zip Function With Examples | Python Unzipping values - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is Zip() Function in Python, Python Zip Function Example, Unzipping Values in Python, Zip(*) Python, Python Zip object\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-zip-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Zip Function With Examples | Python Unzipping values - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is Zip() Function in Python, Python Zip Function Example, Unzipping Values in Python, Zip(*) Python, Python Zip object\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-zip-function\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-23T23:46:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-20T09:10:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Python-Zip-Function-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair 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":"Python Zip Function With Examples | Python Unzipping values - DataFlair","description":"Learn what is Zip() Function in Python, Python Zip Function Example, Unzipping Values in Python, Zip(*) Python, Python Zip object","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:\/\/data-flair.training\/blogs\/python-zip-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Zip Function With Examples | Python Unzipping values - DataFlair","og_description":"Learn what is Zip() Function in Python, Python Zip Function Example, Unzipping Values in Python, Zip(*) Python, Python Zip object","og_url":"https:\/\/data-flair.training\/blogs\/python-zip-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-08-23T23:46:24+00:00","article_modified_time":"2026-04-20T09:10:10+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Python-Zip-Function-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Zip Function With Examples | Python Unzipping values","datePublished":"2018-08-23T23:46:24+00:00","dateModified":"2026-04-20T09:10:10+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/"},"wordCount":557,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Python-Zip-Function-01.jpg","keywords":["numpy zip function","Python zip","Python Zip for loop","Python Zip function","Python Zip Function example","Python Zip list of lists","Python Zip object","syntax for Python Zip Function","Zip Function in Python","Zip(*) Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-zip-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/","url":"https:\/\/data-flair.training\/blogs\/python-zip-function\/","name":"Python Zip Function With Examples | Python Unzipping values - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Python-Zip-Function-01.jpg","datePublished":"2018-08-23T23:46:24+00:00","dateModified":"2026-04-20T09:10:10+00:00","description":"Learn what is Zip() Function in Python, Python Zip Function Example, Unzipping Values in Python, Zip(*) Python, Python Zip object","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-zip-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Python-Zip-Function-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Python-Zip-Function-01.jpg","width":1200,"height":628,"caption":"Python Zip Function With Examples | Python Unzipping values"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-zip-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Zip Function With Examples | Python Unzipping values"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=25863"}],"version-history":[{"count":14,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25863\/revisions"}],"predecessor-version":[{"id":147699,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25863\/revisions\/147699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/25901"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=25863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=25863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=25863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}