{"id":2656,"date":"2026-10-19T11:03:33","date_gmt":"2026-10-19T11:03:33","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=2656"},"modified":"2026-10-20T02:05:58","modified_gmt":"2026-10-20T02:05:58","slug":"python-dictionary-tutorial-for-beginners","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/","title":{"rendered":"Python Dictionary"},"content":{"rendered":"<h1><strong><b>Python Dictionary Tutorial for Beginners<\/b><\/strong><\/h1>\n<p>Open the contacts app on your phone. You do not scroll through numbers to find a person. You look up a name, and the phone gives you the number that belongs to it. A Python dictionary works the same way. It stores information as pairs, a name and its value, and lets you find any value instantly by asking for its name.<\/p>\n<p>Dictionaries are one of the most useful data types in Python. They hold student records, product prices, settings for a program, counts of words in a text, and the data that web services send back as JSON. If you want to write real Python programs, you will use dictionaries in nearly every one.<\/p>\n<p>In this tutorial, we start with what a dictionary is and how to create one. We then read values, change them, add new ones and loop through them, with every example run and its output shown. Next come the dictionary methods: get, values, items, len, pop, popitem, del and copy, plus others that the original version of this page missed, such as keys, update, setdefault and fromkeys. We finish with word counting, dictionary comprehension, nested dictionaries, the shallow copy trap, common errors and an FAQ.<\/p>\n<h2><strong><b>Introduction to Dictionaries in Python<\/b><\/strong><\/h2>\n<p>A dictionary is a collection of <strong><b>key value pairs<\/b><\/strong>\u00a0written inside curly braces. Each key is connected to one value, with a colon between them, and pairs are separated by commas.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">c = {1: \"aa\", 2: \"bba\", 3: \"ab\", 4: \"ba\"}<\/pre>\n<\/div>\n<p>Here the keys are 1, 2, 3 and 4, and the values are the short texts. Keys can be numbers, text, or tuples, and values can be anything at all.<\/p>\n<p>Here is what makes a dictionary a dictionary:<\/p>\n<ul>\n<li><b><\/b><b><\/b>You can change, add and remove items after creating it. The original page called it immutable, which is wrong.<\/li>\n<li><b><\/b><strong><b>Ordered by insertion.<\/b><\/strong>Since Python 3.7, items stay in the order you added them. The original page called it unordered, which is true only for very old versions.<\/li>\n<li><b><\/b><strong><b>Looked up by key, not by position.<\/b><\/strong>You ask for c[2], meaning the value for key 2, not the item in second place.<\/li>\n<li><b><\/b><strong><b>Keys are unique.<\/b><\/strong>Adding a key that already exists replaces its value.<\/li>\n<li><b><\/b><strong><b>Keys must be unchangeable.<\/b><\/strong>Numbers, text and tuples are allowed. Lists are not.<\/li>\n<\/ul>\n<h2><strong><b>Python Dictionary Examples<\/b><\/strong><\/h2>\n<p>All the outputs below came from a real run on Python 3.13. The same code works on every version from 3.7 onward.<\/p>\n<h3><strong><b>Read a Value by Its Key<\/b><\/strong><\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">c = {1: \"aa\", 2: \"bba\", 3: \"ab\", 4: \"ba\"}\r\nprint(c[2])<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">bba<\/pre>\n<\/div>\n<p>Put the key inside square brackets. If the key does not exist, Python raises a KeyError. We will see how to avoid that when we reach the get method.<\/p>\n<h3><strong><b>Change a Value<\/b><\/strong><\/h3>\n<p>Use an equals sign. The original page wrote c[1] : ww with a colon, which does nothing. The colon belongs only inside the curly braces when you create the dictionary.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">c = {1: \"aa\", 2: \"bba\", 3: \"ab\", 4: \"ba\"}\r\nc[1] = \"ww\"\r\nprint(c)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{1: 'ww', 2: 'bba', 3: 'ab', 4: 'ba'}<\/pre>\n<\/div>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/QjYfZk6IQCc\" width=\"850\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h3><strong><b>Add a New Item<\/b><\/strong><\/h3>\n<p>Assigning to a key that does not exist adds it.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">c = {1: \"aa\", 2: \"bba\", 3: \"ab\", 4: \"ba\"}\r\nc[1] = \"ww\"\r\nc[5] = \"new\"\r\nprint(c)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{1: 'ww', 2: 'bba', 3: 'ab', 4: 'ba', 5: 'new'}<\/pre>\n<\/div>\n<h3><strong><b>Loop Through a Dictionary<\/b><\/strong><\/h3>\n<p>A plain loop gives you the keys. Remember that the line inside a loop must be indented, which the original page missed.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">for key in c:\r\n    print(key, end=\" \")\r\nprint()<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">1 2 3 4 5<\/pre>\n<\/div>\n<h2><strong><b>Python Dictionary Methods<\/b><\/strong><\/h2>\n<p>Methods are built in tools that work on a dictionary. Here is the quick map before we try each one.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Dictionary methods at a glance<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 230px;\" scope=\"col\">Method<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">What it does<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Returns<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">get(key)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Reads a value safely<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">The value, or None if missing<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">keys()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Lists the keys<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A view of all keys<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">values()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Lists the values<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A view of all values<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">items()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Lists key and value pairs<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A view of pairs<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">pop(key)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Removes one item by key<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">The removed value<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">popitem()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Removes the last added item<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">The removed pair<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">update(other)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Adds or replaces many items<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None, the dictionary is changed<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">setdefault(key, value)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Reads a key, adding it if missing<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">The value<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">clear()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Removes every item<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">copy()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Makes a shallow copy<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A new dictionary<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">fromkeys(keys, value)<\/th>\n<td style=\"padding: 12px 16px;\">Builds a dictionary from keys<\/td>\n<td style=\"padding: 12px 16px;\">A new dictionary<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><strong><b>get()<\/b><\/strong><\/h3>\n<p>get reads a value by key without crashing when the key is missing. With square brackets, a missing key raises a KeyError. With get, you get None, or a default value that you choose.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(c.get(2))\r\nprint(c.get(9))\r\nprint(c.get(9, \"not found\"))\r\ntry:\r\n    c[9]\r\nexcept KeyError as error:\r\n    print(\"KeyError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">bba\r\nNone\r\nnot found\r\nKeyError: 9<\/pre>\n<\/div>\n<h3><strong><b>values()<\/b><\/strong><\/h3>\n<p>values gives every value. Use it in a loop when you do not need the keys. The original page forgot the colon after the for line.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">for value in c.values():\r\n    print(value, end=\" \")\r\nprint()<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">ww bba ab ba new<\/pre>\n<\/div>\n<h3><strong><b>items()<\/b><\/strong><\/h3>\n<p>items gives each key and value together, so you can unpack both in one loop. The original example printed c[2] every time, which hid what items does. Print the two loop variables instead.<\/p>\n<p>for key, value in c.items():<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">for key, value in c.items():\r\n    print(key, \"-&gt;\", value)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">1 -&gt; ww\r\n2 -&gt; bba\r\n3 -&gt; ab\r\n4 -&gt; ba\r\n5 -&gt; new<\/pre>\n<\/div>\n<h3><strong><b>keys()<\/b><\/strong><\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(list(c.keys()))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">[1, 2, 3, 4, 5]<\/pre>\n<\/div>\n<h3><strong><b>len()<\/b><\/strong><\/h3>\n<p>len counts the items, which means the number of key value pairs. You can also test whether a key exists with the in word, which checks keys and not values.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(len(c), 3 in c, 99 in c)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">5 True False<\/pre>\n<\/div>\n<h3><strong><b>pop()<\/b><\/strong><\/h3>\n<p>pop removes the item with the key you give, and hands back its value.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(c.pop(2))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">bba<\/pre>\n<\/div>\n<h3><strong><b>popitem()<\/b><\/strong><\/h3>\n<p>popitem removes the last item that was added and returns it as a pair. This order is guaranteed from Python 3.7. Before that, it removed an arbitrary item.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(c.popitem())<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">(5, 'new')<\/pre>\n<\/div>\n<h3><strong><b>del<\/b><\/strong><\/h3>\n<p>The original page shows c.del(), which is a syntax error. In Python, del is a statement, and not a method. Use del with a key to remove one item, or del with the dictionary name to delete the whole variable.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">del c[1]\r\nprint(c)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{3: 'ab', 4: 'ba'}<\/pre>\n<\/div>\n<p>After del c, the name c no longer exists, and using it raises a NameError. If you want to empty a dictionary but keep it, use clear.<\/p>\n<h3><strong><b>clear()<\/b><\/strong><\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">c.clear()\r\nprint(c)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{}<\/pre>\n<\/div>\n<h3><strong><b>update()<\/b><\/strong><\/h3>\n<p>update adds the items of another dictionary. If a key already exists, its value is replaced.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">base = {\"name\": \"Asha\", \"city\": \"Kolkata\"}\r\nbase.update({\"city\": \"Pune\", \"age\": 29})\r\nprint(base)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{'name': 'Asha', 'city': 'Pune', 'age': 29}<\/pre>\n<\/div>\n<p>From Python 3.9 you can also merge into a new dictionary with the vertical bar, which leaves the originals alone:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(base | {\"age\": 30})<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{'name': 'Asha', 'city': 'Pune', 'age': 30}<\/pre>\n<\/div>\n<h3><strong><b>setdefault()<\/b><\/strong><\/h3>\n<p>setdefault returns the value for a key. If the key is missing, it adds the key with the default you give, and returns that.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(base.setdefault(\"role\", \"analyst\"))\r\nprint(base.setdefault(\"city\", \"unused\"))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">analyst\r\nPune<\/pre>\n<\/div>\n<p>The first call added role. The second left city alone, because it was already there.<\/p>\n<h3><strong><b>fromkeys()<\/b><\/strong><\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">print(dict.fromkeys([\"a\", \"b\", \"c\"], 0))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{'a': 0, 'b': 0, 'c': 0}<\/pre>\n<\/div>\n<h3><strong><b>copy()<\/b><\/strong><\/h3>\n<p>copy makes a new dictionary with the same items. But it is a shallow copy, which means that any list or dictionary stored as a value is shared between the original and the copy. The original page does not mention this, and it causes real bugs.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">import copy\r\n\r\noriginal = {\"scores\": [80, 90]}\r\nshallow = original.copy()\r\ndeep = copy.deepcopy(original)\r\noriginal[\"scores\"].append(100)\r\nprint(\"shallow:\", shallow)\r\nprint(\"deep:\", deep)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">shallow: {'scores': [80, 90, 100]}\r\ndeep: {'scores': [80, 90]}<\/pre>\n<\/div>\n<p>Adding 100 to the original list also changed the shallow copy, because both point to the same list. The deep copy was not affected. Use copy.deepcopy when your values are lists or dictionaries.<\/p>\n<h2><strong><b>Useful Patterns<\/b><\/strong><\/h2>\n<h3><strong><b>Counting Words<\/b><\/strong><\/h3>\n<p>A very common job is counting how often things occur. The get method makes it short, because it returns 0 for a word seen for the first time.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">words = \"to be or not to be\".split()\r\ncounts = {}\r\nfor word in words:\r\n    counts[word] = counts.get(word, 0) + 1\r\nprint(counts)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{'to': 2, 'be': 2, 'or': 1, 'not': 1}<\/pre>\n<\/div>\n<h3><strong><b>Dictionary Comprehension<\/b><\/strong><\/h3>\n<p>You can build a dictionary in one line, in the same way as a list comprehension.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">squares = {n: n * n for n in range(1, 6)}\r\nprint(squares)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/pre>\n<\/div>\n<h3><strong><b>Nested Dictionaries<\/b><\/strong><\/h3>\n<p>A value can be another dictionary. Use one pair of brackets for each level.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">student = {\"name\": \"Ravi\", \"marks\": {\"maths\": 88, \"science\": 91}}\r\nprint(student[\"marks\"][\"science\"])<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">91<\/pre>\n<\/div>\n<p>This is the shape of most JSON data from the web, so you will meet it often.<\/p>\n<h2><strong><b>Rules for Dictionary Keys<\/b><\/strong><\/h2>\n<p>A key must be hashable, which in practice means it cannot be changed after it is created. Numbers, text and tuples work. A list does not, because a list can change.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">bad = {[1, 2]: \"list\"}\r\nprint({(1, 2): \"tuple works\"})<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">TypeError: unhashable type: 'list'\r\n{(1, 2): 'tuple works'}<\/pre>\n<\/div>\n<h2><strong><b>Common Errors and Mistakes<\/b><\/strong><\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Common dictionary mistakes<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 300px;\" scope=\"col\">Mistake<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 210px;\" scope=\"col\">What happens<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Reading a missing key with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">c[key]<\/code><\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">KeyError<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">c.get(key)<\/code>, or check with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">in<\/code> first<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Using a colon to change a value<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Nothing changes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">c[key] = value<\/code><\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Using a list as a key<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">TypeError, unhashable<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Use a tuple<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Changing a dictionary while looping over it<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">RuntimeError<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Loop over <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">list(c)<\/code> or build a new dictionary<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Treating copy as a full copy<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Nested values are shared<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">copy.deepcopy<\/code><\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Writing <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">c.del()<\/code><\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Syntax error<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">del c[key]<\/code><\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a;\" scope=\"row\">Forgetting indentation inside a loop<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">IndentationError<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Indent the body by 4 spaces<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a;\" scope=\"row\">Duplicate keys in one dictionary<\/th>\n<td style=\"padding: 12px 16px;\">The last one wins silently<\/td>\n<td style=\"padding: 12px 16px;\">Use unique keys<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 24px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 32px 0 12px;\">Dictionary Compared With List, Tuple and Set<\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 640px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Four collection types<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 130px;\" scope=\"col\">Type<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 150px;\" scope=\"col\">Written as<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 130px;\" scope=\"col\">Changeable<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Best for<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">List<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">[1, 2, 3]<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">An ordered collection that grows or shrinks<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Tuple<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">(1, 2, 3)<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Fixed groups of values<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Set<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">{1, 2, 3}<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Unique items and quick membership checks<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Dictionary<\/th>\n<td style=\"padding: 12px 16px; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">{&#8220;a&#8221;: 1}<\/td>\n<td style=\"padding: 12px 16px;\">Yes<\/td>\n<td style=\"padding: 12px 16px;\">Looking up values by a name or key<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<h2><strong><b>Try It Yourself: The Full Program<\/b><\/strong><\/h2>\n<p>The complete program below holds every example in this tutorial. Copy it into a file, run it, and compare your output with what you see above.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #f5f8fc; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">import copy\r\nimport sys\r\n\r\nprint(\"Python\", sys.version_info.major, sys.version_info.minor)\r\n\r\n# Creating a dictionary and reading values\r\nc = {1: \"aa\", 2: \"bba\", 3: \"ab\", 4: \"ba\"}\r\nprint(c[2])\r\nprint(c.get(2))\r\nprint(c.get(9))\r\nprint(c.get(9, \"not found\"))\r\ntry:\r\n    c[9]\r\nexcept KeyError as error:\r\n    print(\"KeyError:\", error)\r\n\r\n# Changing and adding values\r\nc[1] = \"ww\"\r\nc[5] = \"new\"\r\nprint(c)\r\n\r\n# Looping\r\nfor key in c:\r\n    print(key, end=\" \")\r\nprint()\r\nfor value in c.values():\r\n    print(value, end=\" \")\r\nprint()\r\nfor key, value in c.items():\r\n    print(key, \"-&gt;\", value)\r\nprint(list(c.keys()))\r\nprint(len(c), 3 in c, 99 in c)\r\n\r\n# Removing items\r\nprint(c.pop(2))\r\nprint(c.popitem())\r\ndel c[1]\r\nprint(c)\r\nc.clear()\r\nprint(c)\r\n\r\n# Merging and updating\r\nbase = {\"name\": \"Asha\", \"city\": \"Kolkata\"}\r\nbase.update({\"city\": \"Pune\", \"age\": 29})\r\nprint(base)\r\nprint(base | {\"age\": 30})\r\nprint(base.setdefault(\"role\", \"analyst\"))\r\nprint(base.setdefault(\"city\", \"unused\"))\r\nprint(dict.fromkeys([\"a\", \"b\", \"c\"], 0))\r\n\r\n# Copy: shallow against deep\r\noriginal = {\"scores\": [80, 90]}\r\nshallow = original.copy()\r\ndeep = copy.deepcopy(original)\r\noriginal[\"scores\"].append(100)\r\nprint(\"shallow:\", shallow)\r\nprint(\"deep:\", deep)\r\n\r\n# Counting words\r\nwords = \"to be or not to be\".split()\r\ncounts = {}\r\nfor word in words:\r\n    counts[word] = counts.get(word, 0) + 1\r\nprint(counts)\r\n\r\n# Dictionary comprehension and sorting\r\nsquares = {n: n * n for n in range(1, 6)}\r\nprint(squares)\r\nprint(dict(sorted(counts.items(), key=lambda pair: pair[1], reverse=True)))\r\n\r\n# Nested dictionary\r\nstudent = {\"name\": \"Ravi\", \"marks\": {\"maths\": 88, \"science\": 91}}\r\nprint(student[\"marks\"][\"science\"])\r\n\r\n# Keys must be unchangeable\r\ntry:\r\n    bad = {[1, 2]: \"list\"}\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)\r\nprint({(1, 2): \"tuple works\"})<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; background: #ffffff; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre;\">Python 3 13\r\nbba\r\nbba\r\nNone\r\nnot found\r\nKeyError: 9\r\n{1: 'ww', 2: 'bba', 3: 'ab', 4: 'ba', 5: 'new'}\r\n1 2 3 4 5 \r\nww bba ab ba new \r\n1 -&gt; ww\r\n2 -&gt; bba\r\n3 -&gt; ab\r\n4 -&gt; ba\r\n5 -&gt; new\r\n[1, 2, 3, 4, 5]\r\n5 True False\r\nbba\r\n(5, 'new')\r\n{3: 'ab', 4: 'ba'}\r\n{}\r\n{'name': 'Asha', 'city': 'Pune', 'age': 29}\r\n{'name': 'Asha', 'city': 'Pune', 'age': 30}\r\nanalyst\r\nPune\r\n{'a': 0, 'b': 0, 'c': 0}\r\nshallow: {'scores': [80, 90, 100]}\r\ndeep: {'scores': [80, 90]}\r\n{'to': 2, 'be': 2, 'or': 1, 'not': 1}\r\n{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\r\n{'to': 2, 'be': 2, 'or': 1, 'not': 1}\r\n91\r\nTypeError: unhashable type: 'list'\r\n{(1, 2): 'tuple works'}<\/pre>\n<\/div>\n<p>A dictionary is a simple idea that you will use every day: a name, and the value that goes with it. Run the examples, break them on purpose to see the errors, and then use dictionaries to count, group and look up your own data. When you are comfortable, move on to the other Python data types and to working with files and JSON.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/8dND-vrDfrs\" width=\"650\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Dictionary Tutorial for Beginners Open the contacts app on your phone. You do not scroll through numbers to find a person. You look up a name, and the phone gives you the number that belongs to it. A Python dictionary works the same way. It stores information as pairs, a name and its value, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3422,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,1671],"tags":[76],"class_list":["post-2656","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-python-data-types","tag-python-dictionary-tutorial"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Dictionary Tutorial for Beginners With Examples | PrwaTech<\/title>\n<meta name=\"description\" content=\"Learn Python dictionaries from scratch: create, read, change and loop through them, use get, pop, update and copy, with tested examples and outputs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Dictionary Tutorial for Beginners With Examples | PrwaTech\" \/>\n<meta property=\"og:description\" content=\"Learn Python dictionaries from scratch: create, read, change and loop through them, use get, pop, update and copy, with tested examples and outputs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-19T11:03:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-20T02:05:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/\",\"url\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/\",\"name\":\"Python Dictionary Tutorial for Beginners With Examples | PrwaTech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg\",\"datePublished\":\"2026-10-19T11:03:33+00:00\",\"dateModified\":\"2026-10-20T02:05:58+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Learn Python dictionaries from scratch: create, read, change and loop through them, use get, pop, update and copy, with tested examples and outputs.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg\",\"width\":960,\"height\":550,\"caption\":\"Python Dictionary Tutorial\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Dictionary\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Dictionary Tutorial for Beginners With Examples | PrwaTech","description":"Learn Python dictionaries from scratch: create, read, change and loop through them, use get, pop, update and copy, with tested examples and outputs.","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:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/","og_locale":"en_US","og_type":"article","og_title":"Python Dictionary Tutorial for Beginners With Examples | PrwaTech","og_description":"Learn Python dictionaries from scratch: create, read, change and loop through them, use get, pop, update and copy, with tested examples and outputs.","og_url":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-19T11:03:33+00:00","article_modified_time":"2026-10-20T02:05:58+00:00","og_image":[{"width":960,"height":550,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg","type":"image\/jpeg"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/","url":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/","name":"Python Dictionary Tutorial for Beginners With Examples | PrwaTech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg","datePublished":"2026-10-19T11:03:33+00:00","dateModified":"2026-10-20T02:05:58+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Learn Python dictionaries from scratch: create, read, change and loop through them, use get, pop, update and copy, with tested examples and outputs.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Dictionary-Tutorial.jpg","width":960,"height":550,"caption":"Python Dictionary Tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/python-dictionary-tutorial-for-beginners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Dictionary"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=2656"}],"version-history":[{"count":10,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2656\/revisions"}],"predecessor-version":[{"id":12032,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2656\/revisions\/12032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media\/3422"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=2656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=2656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=2656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}