{"id":1521,"date":"2023-02-01T20:00:00","date_gmt":"2023-02-01T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=1521"},"modified":"2023-01-04T17:21:33","modified_gmt":"2023-01-04T08:21:33","slug":"zip","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/zip\/","title":{"rendered":"[Python] How to Use zip() Function"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to use zip() function<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>What is zip function?<\/li><li>Basic usage of zip function<\/li><li>How to zip 3 or more iterables<\/li><li>Case with different number of iterable elements<\/li><li>Convert 1D list to 2D list using zip() function and comprehension<\/li><li>Create dictionary from two lists<\/li><li>Combination of zip() and enumerate() functions<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What is zip function?<\/h2>\n\n\n\n<p>In the beginning, <span class=\"st-mymarker-s\">I explain briefly zip( ) function<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span class=\"st-mymarker-s\">The z<\/span><span class=\"st-mymarker-s\">ip() function is built-in function<\/span> in Python.<\/p>\n\n\n\n<p>So, We can use zip( ) function without &#8220;import&#8221;.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The zip( ) function creates an iterator that will aggregate elements from multiple iterables.<\/p>\n\n\n\n<p>The zip( ) function is often used with the &#8220;for loop&#8221;, for example, to simultaneously get elements from multiple lists.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Basic usage of zip function<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">basic usage of zip(<\/span><span class=\"st-mymarker-s\"> <\/span><span class=\"st-mymarker-s\">) function<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The zip( ) function takes iterables (such as list, tuple, etc.) as the arguments.<\/p>\n\n\n<div class=\"st-mybox  has-title st-mybox-class\" style=\"background:#E3F2FD;border-color:#2196F3;border-width:3px;border-radius:5px;margin: 25px 0 25px 0;\"><p class=\"st-mybox-title\" style=\"color:#757575;font-weight:bold;text-shadow: #fff 3px 0px 0px, #fff 2.83487px 0.981584px 0px, #fff 2.35766px 1.85511px 0px, #fff 1.62091px 2.52441px 0px, #fff 0.705713px 2.91581px 0px, #fff -0.287171px 2.98622px 0px, #fff -1.24844px 2.72789px 0px, #fff -2.07227px 2.16926px 0px, #fff -2.66798px 1.37182px 0px, #fff -2.96998px 0.42336px 0px, #fff -2.94502px -0.571704px 0px, #fff -2.59586px -1.50383px 0px, #fff -1.96093px -2.27041px 0px, #fff -1.11013px -2.78704px 0px, #fff -0.137119px -2.99686px 0px, #fff 0.850987px -2.87677px 0px, #fff 1.74541px -2.43999px 0px, #fff 2.44769px -1.73459px 0px, #fff 2.88051px -0.838246px 0px;background: linear-gradient(0deg,#E3F2FD 0%,#E3F2FD 55%,rgba(0,0,0,0) 55%,rgba(0,0,0,0) 100%);\">How to write<\/p><div class=\"st-in-mybox\">\n<p>zip object = zip( iterator1, iterator2, iterator3 &#8230; )<\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits = &#091;'Apple', 'Banana', 'Orange', 'Grape', 'Lemon']\nnumbers = &#091;1, 2, 3, 4, 5]\n\nzipped = zip(fruits, numbers)\nprint(zipped)\n# &lt;zip object at 0x000001B37853ACC8&gt;\n\nprint(list(zipped))\n# &#091;('Apple', 1), ('Banana', 2), ('Orange', 3), ('Grape', 4), ('Lemon', 5)]<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; print(zipped)<br>&gt; # &lt;zip object at 0x000001B37853ACC8&gt;<\/p>\n\n\n\n<p>A zip object is generated.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; print(list(zip_test))<\/p>\n\n\n\n<p>Since the elements cannot be checked as a zip object, they are converted to list type with the list( ) function.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Use the zip( ) function with &#8220;for loop&#8221;<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">The zip( ) function is often used with &#8220;for loop&#8221;<\/span>.<\/p>\n\n\n\n<p>By using the zip( ) function, elements used within &#8220;for loop&#8221; can be got from multiple lists simultaneously.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example of &#8220;for loop&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits = &#091;'Apple', 'Banana', 'Orange', 'Grape', 'Lemon']\nnumbers = &#091;1, 2, 3, 4, 5]\n\nfor fruit, number in zip(fruits, numbers) :\n    print(fruit, number)\n# Apple 1\n# Banana 2\n# Orange 3\n# Grape 4\n# Lemon 5<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">How to zip 3 or more iterables<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">The zip( ) function can also be used with three or more iterables<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>numbers = &#091;1, 2, 3, 4, 5]\nuppercase = &#091;'A', 'B', 'C', 'D', 'E']\nlowercase = &#091;'a', 'b', 'c', 'd', 'e']\n\nfor n, u, l in zip(numbers, uppercase, lowercase) :\n    print(n, u, l)\n# 1 A a\n# 2 B b\n# 3 C c\n# 4 D d\n# 5 E e<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Case with different number of iterable elements<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">If the number of elements is different, processing is performed according to the smaller number of elements<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>numbers = &#091;1, 2, 3, 4, 5]\nuppercase = &#091;'A', 'B', 'C', 'D']\nlowercase = &#091;'a', 'b', 'c']\n\nfor n, u, l in zip(numbers, uppercase, lowercase) :\n    print(n, u, l)\n# 1 A a\n# 2 B b\n# 3 C c<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Process according to the larger number of elements<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">If you want to process according to the larger number of elements, use the &#8220;zip_longest()&#8221; function in the &#8220;itertools&#8221; module<\/span>.<\/p>\n\n\n\n<p>The &#8220;itertools&#8221; is a standard python library.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>from itertools import zip_longest\n\nnumbers = &#091;1, 2, 3, 4, 5]\nuppercase = &#091;'A', 'B', 'C', 'D']\nlowercase = &#091;'a', 'b', 'c']\n\nfor n, u, l in zip_longest(numbers, uppercase, lowercase) :\n    print(n, u, l)\n# 1 A a\n# 2 B b\n# 3 C c\n# 4 D None\n# 5 None None<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If there are not enough elements, &#8220;None&#8221; is returned.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you want to enter an arbitrary value for a missing element, specify the &#8220;fillvalue&#8221; as argument.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>from itertools import zip_longest\n\nnumbers = &#091;1, 2, 3, 4, 5]\nuppercase = &#091;'A', 'B', 'C', 'D']\nlowercase = &#091;'a', 'b', 'c']\n\nfor n, u, l in zip_longest(numbers, uppercase, lowercase, fillvalue='---') :\n    print(n, u, l)\n# 1 A a\n# 2 B b\n# 3 C c\n# 4 D ---\n# 5 --- ---<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Convert 1D list to 2D list using zip() function and comprehension<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">By using zip( ) function, 1D list can be converted to 2D list<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>numbers = &#091;1, 2, 3, 4, 5]\nuppercase = &#091;'A', 'B', 'C', 'D', 'E']\nlowercase = &#091;'a', 'b', 'c', 'd', 'e']\n\nlist_2d = list(zip(numbers, uppercase, lowercase))\nprint(list_2d)\n# &#091;(1, 'A', 'a'), (2, 'B', 'b'), (3, 'C', 'c'), (4, 'D', 'd'), (5, 'E', 'e')]<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In this example, the elements of the list are tuples.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you want to convert to 2D list, use comprehension.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>numbers = &#091;1, 2, 3, 4, 5]\nuppercase = &#091;'A', 'B', 'C', 'D', 'E']\nlowercase = &#091;'a', 'b', 'c', 'd', 'e']\nlist_2d = &#091; &#091;n, u, l] for n, u, l in zip(numbers, uppercase, lowercase)]\nprint(list_2d)\n# &#091;&#091;1, 'A', 'a'], &#091;2, 'B', 'b'], &#091;3, 'C', 'c'], &#091;4, 'D', 'd'], &#091;5, 'E', 'e']]<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>How to use of comprehension is explained in this article.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/python-comprehension\/\">How to Use Comprehension<\/a><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Create dictionary from two lists<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">By using the zip( ) function, a dictionary can be created from two lists<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The zip( ) function merges the lists of keys and values, and the dict( ) function converts the &#8220;zip object&#8221; to dictionary type.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>numbers = &#091;1, 2, 3, 4, 5]\nfruits = &#091;'Apple', 'Banana', 'Orange', 'Grape', 'Lemon']\n\nfruits_dict = dict(zip(numbers, fruits))\nprint(type(fruits_dict))\n# &lt;class 'dict'&gt;\nprint(fruits_dict)\n# {1: 'Apple', 2: 'Banana', 3: 'Orange', 4: 'Grape', 5: 'Lemon'}\n\nprint(fruits_dict&#091;2])\n# Banana<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Combination of zip() and enumerate() functions<\/h2>\n\n\n\n<p>The enumerate( ) function allows simultaneous getting the elements and indices of an iterable object.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code of the enumerate( ) function.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits = &#091;'Apple', 'Banana', 'Orange', 'Grape', 'Lemon']\n\nfor i, fruit in enumerate(fruits) :\n    print(i, fruit)\n# 0 Apple\n# 1 Banana\n# 2 Orange\n# 3 Grape\n# 4 Lemon<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When using the enumerate() and zip() functions at the same time, the variable must be received as a tuple.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>uppercase = &#091;'A', 'B', 'C', 'D', 'E']\nlowercase = &#091;'a', 'b', 'c', 'd', 'e']\n\nfor i, (u, l) in enumerate(zip(uppercase, lowercase)) :\n    print(i, u, l)\n# 0 A a\n# 1 B b\n# 2 C c\n# 3 D d\n# 4 E e<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Another way of writing<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>uppercase = &#091;'A', 'B', 'C', 'D', 'E']\nlowercase = &#091;'a', 'b', 'c', 'd', 'e']\n\nfor i, value_tuple in enumerate(zip(uppercase, lowercase)) :\n    print(i, value_tuple)\n# 0 ('A', 'a')\n# 1 ('B', 'b')\n# 2 ('C', 'c')\n# 3 ('D', 'd')\n# 4 ('E', 'e')<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>How to use of the enumerate() function is explained in this article.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/enumerate\/\">How to Use enumerate() Function<\/a><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to use zip() function. What is zip function? In the beginning, I explain brie &#8230; <\/p>\n","protected":false},"author":1,"featured_media":2703,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,9],"tags":[28,16],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1521"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=1521"}],"version-history":[{"count":134,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1521\/revisions"}],"predecessor-version":[{"id":3952,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1521\/revisions\/3952"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/2703"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=1521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=1521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=1521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}