{"id":1072,"date":"2023-04-18T22:22:00","date_gmt":"2023-04-18T16:52:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1072"},"modified":"2023-08-15T13:28:51","modified_gmt":"2023-08-15T07:58:51","slug":"access-list-values-within-the-dictionary","status":"publish","type":"post","link":"https:\/\/geekpython.in\/access-list-values-within-the-dictionary","title":{"rendered":"How to Access List Values within the Dictionary in Python"},"content":{"rendered":"\n<p>The&nbsp;<strong>dictionary<\/strong>&nbsp;is a data structure in Python that belongs to the&nbsp;<strong>mapping<\/strong>&nbsp;category. When data(key-value) is enclosed by curly(<code>{ }<\/code>) braces, we can say it is a dictionary.<\/p>\n\n\n\n<p>A dictionary has a&nbsp;<strong>key<\/strong>&nbsp;that holds a&nbsp;<strong>value<\/strong>, also known as a&nbsp;<code>key-value<\/code>&nbsp;pair. Using the&nbsp;<code>dictionary[key]<\/code>, we can get the value assigned to the&nbsp;<code>key<\/code>.<\/p>\n\n\n\n<p><strong>What if the dictionary contains the values in the form of a list?<\/strong>&nbsp;In this article, we&#8217;ll look at all of the different ways to access items from lists within the dictionary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-sample-data\"><a href=\"https:\/\/geekpython.in\/access-list-values-within-the-dictionary#heading-sample-data\"><\/a>Sample data<\/h2>\n\n\n\n<p>Given data contains items in a list as the values of keys within the dictionary. We&#8217;ll work with this given data throughout the article.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"936\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sdata.png\" alt=\"Sample data\" class=\"wp-image-1075\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sdata.png 1920w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sdata-300x146.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sdata-1024x499.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sdata-768x374.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sdata-1536x749.png 1536w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sdata-2048x998.png 2048w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-indexing\"><a href=\"https:\/\/geekpython.in\/access-list-values-within-the-dictionary#heading-using-indexing\"><\/a>Using Indexing<\/h2>\n\n\n\n<p>In the following example, we&#8217;ve used the indexing method and passed the&nbsp;<strong>index<\/strong>&nbsp;along with the&nbsp;<strong>key<\/strong>&nbsp;from which the value is to be extracted.<\/p>\n\n\n\n<p>The convention&nbsp;<code>dict_name[key][index]<\/code>&nbsp;must be used to access the list values from the dictionary.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Using indexing\n\n# Accessing list items from the key\nacc_key = my_data['Python dev']\nprint(acc_key)\n# Accessing first list item from the key\nacc_data = my_data['C++ dev'][0]\nprint(acc_data)\n\n# Accessing third list item from the key\nacc_data1 = my_data['PHP dev'][2]\nprint(acc_data1)\n\n# Accessing second list item from the key 'name' from\n# the dictionary 'detail' within the dictionary 'my_data'\nacc_dict_item = my_data['detail']['name'][1]\nprint(acc_dict_item)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">['Sachin', 'Aman', 'Siya']\nRishu\nShalini\nSiya<\/pre><\/div>\n\n\n\n<p>When we accessed only the&nbsp;<code>key<\/code>, we got the entire list, but when we specified the&nbsp;<code>index<\/code>&nbsp;number with the&nbsp;<code>key<\/code>, we got the specific values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-slicing\"><a href=\"https:\/\/geekpython.in\/access-list-values-within-the-dictionary#heading-using-slicing\"><\/a>Using slicing<\/h2>\n\n\n\n<p>It&#8217;s the same as the previous method, except instead of specifying the index value, we&#8217;ve used the slicing range.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Using slicing method\n\n# Accessing list items by skipping 2 steps\nval = my_data['Python dev'][:3:2]\nprint(val)\n\n# Accessing first list item from the end\nval1 = my_data['C++ dev'][-1:]\nprint(val1)\n\n# Accessing list items except the last item\nval2 = my_data['PHP dev'][:-1]\nprint(val2)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">['Sachin', 'Siya']\n['Rahul']\n['Abhishek', 'Peter']<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-for-loop\"><a href=\"https:\/\/geekpython.in\/access-list-values-within-the-dictionary#heading-using-for-loop\"><\/a>Using for loop<\/h2>\n\n\n\n<p>The for loop has been used in the following example to iterate over the values of the list in the dictionary&nbsp;<code>my_data<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Using for loop\n\n# 1 - Accessing key and values from the dict 'my_data'\nfor key, value in my_data.items():\n    print(key, value)\n\nprint('-'*20)\n\n# 2 - Iterating over list items from each key\nfor key, value in my_data.items():\n    for items in value:\n        print(f'{key} - {items}')\n\nprint('-'*20)\n\n# 3 - Iterating list items from nested dictionary 'detail'\nfor key, value in my_data['detail'].items():\n    for items in value:\n        print(f'{key} - {items}')\n\nprint('-'*20)\n\n# 4 - Accessing list item from individual key\nfor value in my_data['PHP dev']:\n    print(value)<\/pre><\/div>\n\n\n\n<p>We iterated both&nbsp;<code>keys<\/code>&nbsp;and&nbsp;<code>values<\/code>&nbsp;from the dictionary&nbsp;<code>my_data<\/code>&nbsp;in the first block of code, then the list items from each&nbsp;<code>key<\/code>&nbsp;in the second block, the list items from each&nbsp;<code>key<\/code>&nbsp;within the nested dictionary&nbsp;<code>detail<\/code>&nbsp;in the third block, and the list items of the specific&nbsp;<code>key<\/code>&nbsp;in the last block of code.<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Python dev ['Sachin', 'Aman', 'Siya']\nC++ dev ['Rishu', 'Yashwant', 'Rahul']\nPHP dev ['Abhishek', 'Peter', 'Shalini']\ndetail {'name': ['Sachin', 'Siya'], 'hobby': ['Coding', 'Reading']}\n--------------------\nPython dev - Sachin\nPython dev - Aman\nPython dev - Siya\nC++ dev - Rishu\nC++ dev - Yashwant\nC++ dev - Rahul\nPHP dev - Abhishek\nPHP dev - Peter\nPHP dev - Shalini\ndetail - name\ndetail - hobby\n--------------------\nname - Sachin\nname - Siya\nhobby - Coding\nhobby - Reading\n--------------------\nAbhishek\nPeter\nShalini<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-list-comprehension\"><a href=\"https:\/\/geekpython.in\/access-list-values-within-the-dictionary#heading-using-list-comprehension\"><\/a>Using list comprehension<\/h2>\n\n\n\n<p>The list comprehension technique is used in the following example. It&#8217;s the same method as before, but this time we&#8217;ve used the&nbsp;<code>for<\/code>&nbsp;loop within the&nbsp;<code>list<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Using list comprehension\n\n# Accessing list item from the key 'Python dev'\nlst_com = [item for item in my_data['Python dev']]\nprint(lst_com)\n\n# Accessing list item from nested dictionary\nlst_com1 = [item for item in my_data['detail']['name']]\nprint(lst_com1)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">['Sachin', 'Aman', 'Siya']\n['Sachin', 'Siya']<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-unpacking-operator\"><a href=\"https:\/\/geekpython.in\/access-list-values-within-the-dictionary#heading-using-unpacking-operator\"><\/a>Using unpacking(*) operator<\/h2>\n\n\n\n<p>You&#8217;ve probably used the asterisk(<code>*<\/code>) operator for multiplication, exponentiation,&nbsp;<code>**kwargs<\/code>,&nbsp;<code>*args<\/code>, and other things, but we&#8217;re going to use it as an&nbsp;<strong>unpacking operator<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Using unpacking operator\n\n# Accessing values from every key\nusing_unpkg_op = [*my_data.values()]\nprint(using_unpkg_op)\nprint('-'*20)\nprint(using_unpkg_op[0])\n\nprint('-'*20)\n\n# Accessing list items from the key 'Python dev'\nusing_unpkg_op = [*my_data.get('Python dev')]\n# Accessing first item from the list\nprint(using_unpkg_op[0])<\/pre><\/div>\n\n\n\n<p>The expression&nbsp;<code>*my_data.values()<\/code>&nbsp;in the preceding code will return all the values of the keys in the dictionary&nbsp;<code>my_data<\/code>, and once the values are returned, we can specify the index number to access specific data.<\/p>\n\n\n\n<p>The expression&nbsp;<code>*my_data.get('Python dev')<\/code>&nbsp;returns the values of the key&nbsp;<code>Python dev<\/code>, and through indexing, we can access the list item.<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">[['Sachin', 'Aman', 'Siya'], ['Rishu', 'Yashwant', 'Rahul'], ['Abhishek', 'Peter', 'Shalini'], {'name': ['Sachin', 'Siya'], 'hobby': ['Coding', 'Reading']}]\n--------------------\n['Sachin', 'Aman', 'Siya']\n--------------------\nSachin<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/access-list-values-within-the-dictionary#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>In this article, we&#8217;ve seen the different methods to access list items within the dictionary. The dictionary we&#8217;ve operated on contains the keys with the values as a list and also a nested dictionary.<\/p>\n\n\n\n<p>We&#8217;ve used five methods to access the list items from the dictionary which are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Indexing<\/strong>&nbsp;&#8211; Using the bracket notation(<code>[ ]<\/code>)<\/li>\n\n\n\n<li><strong>Slicing<\/strong>&nbsp;&#8211; Using the list slicing<\/li>\n\n\n\n<li><strong>Iterating<\/strong>&nbsp;&#8211; Using the&nbsp;<code>for<\/code>&nbsp;loop<\/li>\n\n\n\n<li><strong>List comprehension technique<\/strong><\/li>\n\n\n\n<li><strong>Unpacking(<\/strong><code>*<\/code><strong>) operator<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Well, this is it for the article, try to find some more methods to access list items within the dictionary.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python\" rel=\"noreferrer noopener\">How list reverse() is different from the reversed() in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list\" rel=\"noreferrer noopener\">8 different ways to reverse the Python list<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/one-liners-in-python\" rel=\"noreferrer noopener\">Python one-liners that will make your code more efficient<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/numpy-argmax-function-in-python\" rel=\"noreferrer noopener\">Understanding NumPy argmax function in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/how-to-open-and-read-multiple-files-using-with-in-python\" rel=\"noreferrer noopener\">How to read multiple files simultaneously using the with statement in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/asyncio-how-to-use-asyncawait-in-python\" rel=\"noreferrer noopener\">Using asynchronous(async\/await) programming in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/ways-to-remove-whitespaces-from-the-string-in-python-with-examples-beginners-guide\" rel=\"noreferrer noopener\">Different ways to remove whitespaces in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;dictionary&nbsp;is a data structure in Python that belongs to the&nbsp;mapping&nbsp;category. When data(key-value) is enclosed by curly({ }) braces, we can say it is a dictionary. A dictionary has a&nbsp;key&nbsp;that holds a&nbsp;value, also known as a&nbsp;key-value&nbsp;pair. Using the&nbsp;dictionary[key], we can get the value assigned to the&nbsp;key. What if the dictionary contains the values in the form [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1074,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,7],"tags":[12,31],"class_list":["post-1072","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-python-tips","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1072"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1072\/revisions"}],"predecessor-version":[{"id":1296,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1072\/revisions\/1296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1074"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}