{"id":1010,"date":"2017-11-30T17:40:37","date_gmt":"2017-11-30T22:40:37","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1010"},"modified":"2017-11-30T17:40:37","modified_gmt":"2017-11-30T22:40:37","slug":"python-how-to-build-a-dictionary-with-two-lists-using-zip","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/11\/30\/python-how-to-build-a-dictionary-with-two-lists-using-zip\/","title":{"rendered":"Python: How to build a dictionary with two lists using zip( )"},"content":{"rendered":"<p>If you have two lists and would like to build a dictionary where one list will be the keys and the second list will be the values, Python has a way to do this easily using the built-in<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">zip()<\/code> function.\u00a0 With small enough examples, we could build our dictionary manually by typing it at the keyboard. However, with large data sets thousands of records long, this is not practical, which is why this technique is so useful.<\/p>\n<p>For example, let&#8217;s say we want to build a dictionary of fruit where the keys are the letters of the alphabet and the values are the names of the fruit. Perhaps these key-value associations are already present in a table or spreadsheet somewhere, and we can access the columns as lists:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">alphabet = [\"a\", \"b\", \"c\", \"d\"]\r\nfruit = [\"apple\", \"banana\", \"cherry\", \"date\"]<\/pre>\n<p>Here is the syntax for this technique:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">my_dictionary = dict(zip(keys_list, values_list))<\/pre>\n<p>Here is the code for applying it to my two example lists. Notice you must specify that you want to create a dictionary using the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">dict()<\/code> function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">alphabet = [\"a\", \"b\", \"c\", \"d\"]\r\nfruit = [\"apple\", \"banana\", \"cherry\", \"date\"]\r\nfruit_dict = dict(zip(alphabet, fruit))\r\nprint fruit_dict<\/pre>\n<p>Which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'date'}<\/pre>\n<p>Note: If there is a mismatch in the number of elements in both lists (one list is longer), then zip will only match elements one for one up to the point where there are values in both lists. For example, if my fruit list had an extra fruit that didn&#8217;t match up to a letter:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">fruit = [\"apple\", \"banana\", \"cherry\", \"date\", \"grape\" ]<\/pre>\n<p>The resulting dictionary would be identical to the first example. &#8220;grape&#8221; would not be in the dictionary since it has no corresponding key in the first list. The point to remember is that the two lists will be zipped together into key-value pairs index by index, so the order of the two lists matters.<\/p>\n<p>For more information, see:<br \/>\n<a href=\"https:\/\/docs.python.org\/2\/library\/functions.html#zip\">https:\/\/docs.python.org\/2\/library\/functions.html#zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have two lists and would like to build a dictionary where one list will be the keys and the second list will be the values, Python has a way to do this easily using the built-inzip() function.\u00a0 With small enough examples, we could build our dictionary manually by typing it at the keyboard. &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/11\/30\/python-how-to-build-a-dictionary-with-two-lists-using-zip\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python: How to build a dictionary with two lists using zip( )<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[4,70],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-python-language","tag-python","tag-zip"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":7,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1017,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1010\/revisions\/1017"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}