{"id":34586,"date":"2023-04-05T07:47:12","date_gmt":"2023-04-05T02:17:12","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/"},"modified":"2023-04-05T07:47:12","modified_gmt":"2023-04-05T02:17:12","slug":"solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/","title":{"rendered":"[Solved] Splitting dict by multi values and one of the keys [closed]"},"content":{"rendered":"<h2 id=\"Introduction\">Introduction<\/h2>\n<p>[ad_1]<\/p>\n<p>Splitting a dictionary by multiple values and one of the keys is a useful way to organize data. It allows you to quickly and easily access specific information from a large set of data. This technique can be used to create subsets of data based on certain criteria, such as a specific key or multiple values. By splitting a dictionary by multiple values and one of the keys, you can quickly and easily access the information you need.<\/p>\n<h2 id=\"solution\"> Solution<\/h2>\n<p>Assuming you have a dictionary like this:<\/p>\n<p>d = {&#8216;a&#8217;: [1, 2, 3], &#8216;b&#8217;: [4, 5, 6], &#8216;c&#8217;: [7, 8, 9]}<\/p>\n<p>You can use a dictionary comprehension to split the dictionary into multiple dictionaries based on the values of one of the keys:<\/p>\n<p>d1 = {k: v for k, v in d.items() if v[0] == 1 or v[0] == 4 or v[0] == 7}<br \/>\nd2 = {k: v for k, v in d.items() if v[0] == 2 or v[0] == 5 or v[0] == 8}<br \/>\nd3 = {k: v for k, v in d.items() if v[0] == 3 or v[0] == 6 or v[0] == 9}<\/p>\n<p>print(d1) # {&#8216;a&#8217;: [1, 2, 3], &#8216;b&#8217;: [4, 5, 6]}<br \/>\nprint(d2) # {&#8216;a&#8217;: [2, 3], &#8216;b&#8217;: [5, 6], &#8216;c&#8217;: [8, 9]}<br \/>\nprint(d3) # {&#8216;a&#8217;: [3], &#8216;b&#8217;: [6], &#8216;c&#8217;: [9]} <\/p>\n<p>[ad_2]<br \/>\n<\/p>\n<div id=\"answer-55977603\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"55977603\" data-parentid=\"55977564\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You cannot have the same key more than once in a dictionary (how would you access it by key?).<\/p>\n<p>You could extract a list of tuples instead, like this:<\/p>\n<pre><code>exploded = [(key, value) for key, values in data.items() for value in values]\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[('id', 123), ('id', 456), ('id', 546), ('id', 311), \n ('info', 'info1'), ('info', 'info2'), ('info', 'info3')]\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<div id=\"end\">  <\/div>\n<h1>Splitting Dict by Multi Values and One of the Keys<\/h1>\n<p>Splitting a dictionary by multiple values and one of the keys can be a tricky task. Fortunately, Python provides a number of ways to accomplish this. In this article, we&#8217;ll look at three different methods for splitting a dictionary by multiple values and one of the keys.<\/p>\n<h2>Method 1: Using the Dictionary Comprehension<\/h2>\n<p>The first method we&#8217;ll look at is using the dictionary comprehension. This method allows us to create a new dictionary from an existing one, based on certain criteria. To use this method, we&#8217;ll need to define a function that takes in the dictionary and returns a boolean value. This boolean value will determine whether or not the key-value pair should be included in the new dictionary.<\/p>\n<p>For example, let&#8217;s say we have a dictionary with the following key-value pairs:<\/p>\n<pre>\nmy_dict = {\n    'name': 'John',\n    'age': 25,\n    'gender': 'male'\n}\n<\/pre>\n<p>We can use the dictionary comprehension to create a new dictionary that only includes the key-value pairs where the value is equal to &#8216;male&#8217;:<\/p>\n<pre>\nmale_dict = {k:v for (k,v) in my_dict.items() if v == 'male'}\n<\/pre>\n<p>This will create a new dictionary with the following key-value pairs:<\/p>\n<pre>\nmale_dict = {\n    'gender': 'male'\n}\n<\/pre>\n<h2>Method 2: Using the Filter Function<\/h2>\n<p>The second method we&#8217;ll look at is using the filter function. This method allows us to filter a dictionary based on a certain criteria. To use this method, we&#8217;ll need to define a function that takes in the dictionary and returns a boolean value. This boolean value will determine whether or not the key-value pair should be included in the new dictionary.<\/p>\n<p>For example, let&#8217;s say we have a dictionary with the following key-value pairs:<\/p>\n<pre>\nmy_dict = {\n    'name': 'John',\n    'age': 25,\n    'gender': 'male'\n}\n<\/pre>\n<p>We can use the filter function to create a new dictionary that only includes the key-value pairs where the value is equal to &#8216;male&#8217;:<\/p>\n<pre>\nmale_dict = dict(filter(lambda item: item[1] == 'male', my_dict.items()))\n<\/pre>\n<p>This will create a new dictionary with the following key-value pairs:<\/p>\n<pre>\nmale_dict = {\n    'gender': 'male'\n}\n<\/pre>\n<h2>Method 3: Using the List Comprehension<\/h2>\n<p>The third method we&#8217;ll look at is using the list comprehension. This method allows us to create a new list from an existing one, based on certain criteria. To use this method, we&#8217;ll need to define a function that takes in the list and returns a boolean value. This boolean value will determine whether or not the item should be included in the new list.<\/p>\n<p>For example, let&#8217;s say we have a list with the following items:<\/p>\n<pre>\nmy_list = [\n    ('name', 'John'),\n    ('age', 25),\n    ('gender', 'male')\n]\n<\/pre>\n<p>We can use the list comprehension to create a new list that only includes the items where the second value is equal to &#8216;male&#8217;:<\/p>\n<pre>\nmale_list = [item for item in my_list if item[1] == 'male']\n<\/pre>\n<p>This will create a new list with the following items:<\/p>\n<pre>\nmale_list = [\n    ('gender', 'male')\n]\n<\/pre>\n<p>These are just three of the many ways to split a dictionary by multiple values and one of the keys. Depending on your specific needs, you may find one of these methods more suitable than the others.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction [ad_1] Splitting a dictionary by multiple values and one of the keys is a useful way to organize data. It allows you to quickly and easily access specific information from a large set of data. This technique can be used to create subsets of data based on certain criteria, such as a specific key &#8230; <a title=\"[Solved] Splitting dict by multi values and one of the keys [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\" aria-label=\"More on [Solved] Splitting dict by multi values and one of the keys [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[834,6078,349],"class_list":["post-34586","post","type-post","status-publish","format-standard","hentry","category-solved","tag-dictionary","tag-multivalue","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Splitting dict by multi values and one of the keys [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Splitting dict by multi values and one of the keys [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"Introduction [ad_1] Splitting a dictionary by multiple values and one of the keys is a useful way to organize data. It allows you to quickly and easily access specific information from a large set of data. This technique can be used to create subsets of data based on certain criteria, such as a specific key ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-05T02:17:12+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Splitting dict by multi values and one of the keys [closed]\",\"datePublished\":\"2023-04-05T02:17:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\"},\"wordCount\":670,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"dictionary\",\"multivalue\",\"python\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\",\"name\":\"[Solved] Splitting dict by multi values and one of the keys [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-04-05T02:17:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Splitting dict by multi values and one of the keys [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Splitting dict by multi values and one of the keys [closed] - JassWeb","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:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Splitting dict by multi values and one of the keys [closed] - JassWeb","og_description":"Introduction [ad_1] Splitting a dictionary by multiple values and one of the keys is a useful way to organize data. It allows you to quickly and easily access specific information from a large set of data. This technique can be used to create subsets of data based on certain criteria, such as a specific key ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/","og_site_name":"JassWeb","article_published_time":"2023-04-05T02:17:12+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Splitting dict by multi values and one of the keys [closed]","datePublished":"2023-04-05T02:17:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/"},"wordCount":670,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["dictionary","multivalue","python"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/","name":"[Solved] Splitting dict by multi values and one of the keys [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-04-05T02:17:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-splitting-dict-by-multi-values-and-one-of-the-keys-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Splitting dict by multi values and one of the keys [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/34586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=34586"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/34586\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=34586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=34586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=34586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}