{"id":910,"date":"2024-09-15T15:38:51","date_gmt":"2024-09-15T15:38:51","guid":{"rendered":"https:\/\/www.zframez.com\/articles\/?p=910"},"modified":"2024-10-16T15:57:27","modified_gmt":"2024-10-16T15:57:27","slug":"chapter-9-python-dictionaries-operations-and-methods","status":"publish","type":"post","link":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods","title":{"rendered":"Chapter 9: Python Dictionaries &#8211; Operations and Methods"},"content":{"rendered":"<body>\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-8-python-lists-and-tuples-operations-and-methods\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n\n\n\n\n<h1 class=\"wp-block-heading has-x-large-font-size\"><strong>Working with Dictionaries in Python<\/strong><\/h1>\n\n\n\n<p>In Chapter 9, we\u2019ll explore Python dictionaries, a data structure where each value is indexed using a key. Unlike lists, which use numerical indices, dictionaries allow you to use descriptive keys, making them more flexible for storing and accessing data. This concept is similar to what other programming languages call associative arrays or hash maps. In this chapter, we\u2019ll go over different ways to create dictionaries, how to add and remove key-value pairs, and useful dictionary methods like <code>get()<\/code>, <code>keys()<\/code>, and <code>values()<\/code>. We\u2019ll also look at how to create two-dimensional dictionaries using lists, nested dictionaries, and the <code>defaultdict<\/code> class from the collections module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Different Ways of Creating a Dictionary<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-c091cb876be3065b037783767f86fabe\"><code>a = {'joe': 85, 'peter': 88, 'jack': 90}\nb = dict(joe=90, peter=85, jack=88)\nc = dict([('joe', 90), ('peter', 85), ('jack', 95)])  # Using a list of tuples\n\nprint(a)             # {'joe': 85, 'peter': 88, 'jack': 90}\nprint(type(a))       # &lt;class 'dict'&gt;\nprint(len(a))        # 3\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>There are multiple ways to create dictionaries in Python. You can use curly braces <code>{}<\/code> with key-value pairs, the <code>dict()<\/code> constructor, or pass a list of tuples to <code>dict()<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Adding and Removing Key-Value Pairs<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-cd80c7395d4238cc407333b0070b2fc6\"><code>a['kris'] = 85         # Adding a new key-value pair\nprint(a)               # {'joe': 85, 'peter': 88, 'jack': 90, 'kris': 85}\n\ndel a['kris']          # Deleting a key-value pair\nprint(a)               # {'joe': 85, 'peter': 88, 'jack': 90}\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can easily add new key-value pairs to a dictionary by assigning a value to a new key. The <code>del<\/code> keyword is used to remove key-value pairs from the dictionary.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Checking for Key Presence<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-063bc339e50f9443a019d091aec2c38f\"><code>print('peter' in a)     # True\nprint('michel' not in a)  # True<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>in<\/code> and <code>not in<\/code> operators are used to check whether a specific key is present in the dictionary.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Dictionary Methods<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-f07d64d59b43a0e14f3b0991ac0e138a\"><code>a = {'joe': 85, 'peter': 88, 'jack': 90}\n\n# Get the value of a key, with a default if the key is not found\nb = a.get('peter', 0)\nprint(b)                # 88\nc = a.get('kris', 0)\nprint(c)                # 0\n\n# Get all the keys and values from the dictionary\nkeys = a.keys()\nvalues = a.values()\nprint(keys)             # dict_keys(['joe', 'peter', 'jack'])\nprint(list(values))     # [85, 88, 90]<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>get()<\/code> method returns the value for the given key, or a default value (0 in this case) if the key is not found.<\/li>\n\n\n\n<li><code>keys()<\/code> and <code>values()<\/code> return the keys and values of the dictionary, respectively.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Creating Two-Dimensional Dictionaries<\/strong><\/h2>\n\n\n\n<p>Method 1: <strong>Using Lists as Values:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-7d26025189600e1ba4b5d490774be088\"><code>marks = {'peter': [80, 85, 90], 'jose': [85, 90, 95]}\nprint(marks['jose'])         # [85, 90, 95]\nprint(marks['jose'][0])      # 85\n\n# Updating a value inside the list\nmarks['jose'][0] = 86\nprint(marks)                 # {'peter': [80, 85, 90], 'jose': [86, 90, 95]}<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this example, the dictionary stores lists as values, effectively creating a two-dimensional dictionary where rows are represented by the keys (<code>'peter'<\/code>, <code>'jose'<\/code>) and columns by list indices.<\/li>\n<\/ul>\n\n\n\n<p>Method 2: <strong>Using Dictionaries as Values<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-d5b56545d41ef463a64095fcd5bee774\"><code>marks2 = {\n    'jose': {'maths': 85, 'english': 90, 'science': 95},\n    'peter': {'maths': 80, 'english': 85, 'science': 90}\n}\nprint(marks2['jose']['science'])    # 95\n\n# Updating a value inside the dictionary\nmarks2['jose']['science'] = 96\nprint(marks2)                       # {'jose': {'maths': 85, 'english': 90, 'science': 96}, 'peter': {'maths': 80, 'english': 85, 'science': 90}}<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this method, dictionaries are used as values, providing a more descriptive structure. This allows for nested dictionaries where keys represent both rows and columns.<\/li>\n<\/ul>\n\n\n\n<p>Method 3: <strong>Using <code>defaultdict<\/code> from <code>collections<\/code><\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-7e28373e202abf07b73b0336abda4ea5\"><code>from collections import defaultdict\n\nmarks3 = defaultdict(dict)\nmarks3['jose']['maths'] = 85\nmarks3['jose']['science'] = 95\nmarks3['peter']['maths'] = 80\nmarks3['peter']['english'] = 85\n\nprint(marks3)                    \n\n# defaultdict(&lt;class 'dict'&gt;, {'jose': {'maths': 85, 'science': 95}, 'peter': {'maths': 80, 'english': 85}})\nprint(marks3['jose']['science'])  # 95<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>defaultdict<\/code> <\/strong>automatically initializes missing keys with a default value. In this case, the default value is an empty dictionary, allowing easy creation of nested dictionaries.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Programming Exercises \/ Interview Questions<\/strong> :<\/h2>\n\n\n\n<p>Following are the programs you need to practice to get a strong understanding of this topic or to prepare for interviews.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Write a Python program to print all the keys and values from a given dictionary.<\/li>\n\n\n\n<li>Write a Python program to find and print the key(s) that correspond to a given value.<\/li>\n\n\n\n<li>Write a Python program to remove a key-value pair from the dictionary based on a given key.<\/li>\n\n\n\n<li>Write a Python program to find the length of a dictionary without using the <code>len()<\/code> function.<\/li>\n\n\n\n<li>Write a Python program to create a dictionary where keys are numbers from 1 to 5, and the values are squares of the keys.<\/li>\n\n\n\n<li>Write a Python program to update the value of a specific key in a dictionary.<\/li>\n\n\n\n<li>Write a Python program to create a dictionary from a list of tuples, where each tuple contains two elements: a key and a value. <\/li>\n\n\n\n<li>Write a Python program to invert a dictionary, swapping the keys and values. If multiple keys have the same value, store the keys in a list.<\/li>\n\n\n\n<li>Write a Python program to merge two dictionaries. If a key exists in both dictionaries, sum the values associated with that key.<\/li>\n\n\n\n<li>Write a Python program to count the frequency of each word in a given paragraph and store the result in a dictionary.<\/li>\n\n\n\n<li>Write a Python program to filter a dictionary and keep only the entries where the value is greater than a specified threshold.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted table-of-contents has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-297958c4c4b01ed79ca00103ba5f6ae1\">Keys   = (name)    (IP)      (username)  (pwd)<br>Values = Router1   1.1.1.1   zframez   zframez1234<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a dictionary to store the information given above and :\n<ol start=\"12\" class=\"wp-block-list\">\n<li>Write a Python program to print the value associated with a given key.<\/li>\n\n\n\n<li>Write a Python program to check if a given key is present in the dictionary. If the key is present, print its value; otherwise, add a new key-value pair to the dictionary.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted table-of-contents has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-129400d4bbcd36a4a33a0976ff5beb31\">   Interface\tIP\t       Status<br>1  Ethernet0\t1.1.1.1\t       up<br>2  Ethernet1\t2.2.2.2\t       down<br>3  Serial0\t3.3.3.3\t       up<br>4  Serial1\t4.4.4.4\t       up<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a two-dimensional dictionary to store the information in the table given above and : \n<ol start=\"14\" class=\"wp-block-list\">\n<li>Write a Python program to find and print the status of a given interface.<\/li>\n\n\n\n<li>Write a Python program to find and print the interface names and IP addresses of all interfaces that are \u201cup.\u201d<\/li>\n\n\n\n<li>Write a Python program to count and print how many Ethernet interfaces are in the database.<\/li>\n\n\n\n<li>Write a Python program to add a new entry to the above database.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\">\n\n\n\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-8-python-lists-and-tuples-operations-and-methods\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n\n<\/body>","protected":false},"excerpt":{"rendered":"<p>\u25c0 Previous Python Tutorials Next \u25b6 Working with Dictionaries in Python In Chapter 9, we\u2019ll explore Python dictionaries, a data structure where each value is indexed using a key. Unlike lists, which use numerical indices, dictionaries allow you to use descriptive keys, making them more flexible for storing and accessing data. This concept is similar [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-post-with-sidebar2","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[46],"tags":[309,306,305,304,307,303,50,47,308],"class_list":["post-910","post","type-post","status-publish","format-standard","hentry","category-python-tutorials","tag-dictionary-methods-in-python","tag-python-collections","tag-python-defaultdict","tag-python-dictionaries","tag-python-get-method","tag-python-keys-and-values","tag-python-programming","tag-python-tutorial","tag-two-dimensional-dictionaries"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Chapter 9: Python Dictionaries - Operations and Methods - Tutorials<\/title>\n<meta name=\"description\" content=\"Learn how to work with dictionaries in Python. This chapter covers creating dictionaries, adding and removing key-value pairs, dictionary methods, and two-dimensional dictionaries\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Chapter 9: Python Dictionaries \u2013 Operations and Methods\" \/>\n<meta property=\"og:description\" content=\"Explore Python dictionaries, a flexible data structure that uses keys to index values. Learn key operations, methods, and how to create two-dimensional dictionaries in this chapter\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods\" \/>\n<meta property=\"og:site_name\" content=\"Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/zframez\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-15T15:38:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-16T15:57:27+00:00\" \/>\n<meta name=\"author\" content=\"sajith achipra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@zframez\" \/>\n<meta name=\"twitter:site\" content=\"@zframez\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"sajith achipra\" \/>\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:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods\"},\"author\":{\"name\":\"sajith achipra\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\"},\"headline\":\"Chapter 9: Python Dictionaries &#8211; Operations and Methods\",\"datePublished\":\"2024-09-15T15:38:51+00:00\",\"dateModified\":\"2024-10-16T15:57:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods\"},\"wordCount\":713,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"keywords\":[\"dictionary methods in Python\",\"Python collections\",\"Python defaultdict\",\"Python dictionaries\",\"Python get method\",\"Python keys and values\",\"python programming\",\"python tutorial\",\"two-dimensional dictionaries\"],\"articleSection\":[\"python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods\",\"name\":\"Chapter 9: Python Dictionaries - Operations and Methods - Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-09-15T15:38:51+00:00\",\"dateModified\":\"2024-10-16T15:57:27+00:00\",\"description\":\"Learn how to work with dictionaries in Python. This chapter covers creating dictionaries, adding and removing key-value pairs, dictionary methods, and two-dimensional dictionaries\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-9-python-dictionaries-operations-and-methods#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/www.zframez.com\\\/articles\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Chapter 9: Python Dictionaries &#8211; Operations and Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"name\":\"zframez tutorials\",\"description\":\"Learn networking bit by bit\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\",\"name\":\"zframez technologies\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"width\":864,\"height\":864,\"caption\":\"zframez technologies\"},\"image\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/zframez\\\/\",\"https:\\\/\\\/x.com\\\/zframez\",\"https:\\\/\\\/www.instagram.com\\\/zframez_technologies\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\",\"name\":\"sajith achipra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"caption\":\"sajith achipra\"},\"sameAs\":[\"http:\\\/\\\/www.zframez.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Chapter 9: Python Dictionaries - Operations and Methods - Tutorials","description":"Learn how to work with dictionaries in Python. This chapter covers creating dictionaries, adding and removing key-value pairs, dictionary methods, and two-dimensional dictionaries","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:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods","og_locale":"en_US","og_type":"article","og_title":"Chapter 9: Python Dictionaries \u2013 Operations and Methods","og_description":"Explore Python dictionaries, a flexible data structure that uses keys to index values. Learn key operations, methods, and how to create two-dimensional dictionaries in this chapter","og_url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods","og_site_name":"Tutorials","article_publisher":"https:\/\/www.facebook.com\/zframez\/","article_published_time":"2024-09-15T15:38:51+00:00","article_modified_time":"2024-10-16T15:57:27+00:00","author":"sajith achipra","twitter_card":"summary_large_image","twitter_creator":"@zframez","twitter_site":"@zframez","twitter_misc":{"Written by":"sajith achipra","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods#article","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods"},"author":{"name":"sajith achipra","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41"},"headline":"Chapter 9: Python Dictionaries &#8211; Operations and Methods","datePublished":"2024-09-15T15:38:51+00:00","dateModified":"2024-10-16T15:57:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods"},"wordCount":713,"commentCount":0,"publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"keywords":["dictionary methods in Python","Python collections","Python defaultdict","Python dictionaries","Python get method","Python keys and values","python programming","python tutorial","two-dimensional dictionaries"],"articleSection":["python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods","url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods","name":"Chapter 9: Python Dictionaries - Operations and Methods - Tutorials","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/#website"},"datePublished":"2024-09-15T15:38:51+00:00","dateModified":"2024-10-16T15:57:27+00:00","description":"Learn how to work with dictionaries in Python. This chapter covers creating dictionaries, adding and removing key-value pairs, dictionary methods, and two-dimensional dictionaries","breadcrumb":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Tutorials","item":"https:\/\/www.zframez.com\/articles"},{"@type":"ListItem","position":2,"name":"Chapter 9: Python Dictionaries &#8211; Operations and Methods"}]},{"@type":"WebSite","@id":"https:\/\/www.zframez.com\/articles\/#website","url":"https:\/\/www.zframez.com\/articles\/","name":"zframez tutorials","description":"Learn networking bit by bit","publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.zframez.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.zframez.com\/articles\/#organization","name":"zframez technologies","url":"https:\/\/www.zframez.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","width":864,"height":864,"caption":"zframez technologies"},"image":{"@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/zframez\/","https:\/\/x.com\/zframez","https:\/\/www.instagram.com\/zframez_technologies\/"]},{"@type":"Person","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41","name":"sajith achipra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","caption":"sajith achipra"},"sameAs":["http:\/\/www.zframez.com"]}]}},"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":83,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-1-introduction-to-python-programming","url_meta":{"origin":910,"position":0},"title":"Chapter 1: Introduction to Python Programming","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In this Python tutorial, we'll explore Python's origins, key milestones, and its standout features and benefits. Whether you're new to programming or looking to expand your skills, this guide will provide you with a solid foundation in Python. If you want to understand why\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Table filled with Python-related materials, including a computer displaying Python code, a Python textbook, and notes about Python","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":904,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-7-working-with-strings-in-python","url_meta":{"origin":910,"position":1},"title":"Chapter 7: Working with Strings in Python","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Python String Manipulation: Methods and Examples In Chapter 7, we\u2019ll be looking at how to work with strings in Python. Strings are used everywhere in programming, and Python provides a lot of methods to make string manipulation easy. In this chapter, we\u2019ll explore methods\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":908,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-8-python-lists-and-tuples-operations-and-methods","url_meta":{"origin":910,"position":2},"title":"Chapter 8: Python Lists and Tuples -Operations and Methods","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Working with Lists and Tuples in Python In this chapter, we\u2019ll explore two important data structures in Python: lists and tuples. Lists allow you to store a collection of items that can be modified, while tuples are immutable, meaning their elements cannot be changed.\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":901,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","url_meta":{"origin":910,"position":3},"title":"Chapter 6: Python While and For Loops: Examples and Explanations","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Python While and For Loops In this chapter of our Python tutorial series, we will explore two essential looping structures in Python: while loops and for loops. Loops are a fundamental part of programming, allowing you to execute a block of code repeatedly under\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":90,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-2-setting-up-python","url_meta":{"origin":910,"position":4},"title":"Chapter 2: Setting Up Python","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Before you can start writing and running Python code, you\u2019ll need to set up Python on your computer. In this chapter, we\u2019ll guide you through the process of installing Python on Windows, macOS, and Linux. We\u2019ll also explain what an Integrated Development Environment (IDE)\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Screenshot of the official Python download page, highlighting the process of setting up Python.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":98,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-3-working-with-variables-in-python","url_meta":{"origin":910,"position":5},"title":"Chapter 3: Working with Variables in Python","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In Python, variables are one of the fundamental concepts you'll work with. They allow you to store data that your programs can manipulate and use. In this chapter, we\u2019ll explore what variables are, how to check the Python version you\u2019re using, and the different\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Computer displaying a program, with a book and pen on the table, representing learning and coding in Python.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/comments?post=910"}],"version-history":[{"count":5,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/910\/revisions"}],"predecessor-version":[{"id":1086,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/910\/revisions\/1086"}],"wp:attachment":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/media?parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/categories?post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/tags?post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}