{"id":76,"date":"2026-03-01T16:58:37","date_gmt":"2026-03-01T11:58:37","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=76"},"modified":"2026-03-13T12:34:36","modified_gmt":"2026-03-13T07:34:36","slug":"dictionaries","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/","title":{"rendered":"Dictionaries"},"content":{"rendered":"\n<p>A <strong>dictionary<\/strong> is a collection data type in Python used to store data in <strong>key-value pairs<\/strong>.<br>Dictionaries are <strong>ordered (Python 3.7+)<\/strong>, <strong>mutable<\/strong>, and <strong>do not allow duplicate keys<\/strong>.<\/p>\n\n\n\n<p>They are useful for storing related information together.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>CREATING A DICTIONARY<\/strong><\/h1>\n\n\n\n<p>Dictionaries are created using curly brackets <code>{ }<\/code> with key-value pairs separated by colons <code>:<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">student = {<br>    \"name\": \"Hira\",<br>    \"age\": 25,<br>    \"course\": \"Python\"<br>}<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>ACCESSING VALUES<\/strong><\/h1>\n\n\n\n<p>You can access dictionary values using their keys.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(student[\"name\"])<\/pre>\n\n\n\n<p>Using the <code>get()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(student.get(\"age\"))<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>ADDING OR UPDATING ITEMS<\/strong><\/h1>\n\n\n\n<p>You can add a new key-value pair or update an existing one.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">student[\"city\"] = \"Karachi\"<br>student[\"age\"] = 26<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>REMOVING ITEMS<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">pop() \u2013 Removes item by key<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">student.pop(\"course\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">popitem() \u2013 Removes last inserted item<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">student.popitem()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">del \u2013 Deletes specific key<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">del student[\"city\"]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">clear() \u2013 Removes all items<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">student.clear()<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>LOOPING THROUGH A DICTIONARY<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Loop through keys<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">for key in student:<br>    print(key)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Loop through values<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">for value in student.values():<br>    print(value)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Loop through key-value pairs<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">for key, value in student.items():<br>    print(key, \":\", value)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>DICTIONARY METHODS<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">print(student.keys())<br>print(student.values())<br>print(student.items())<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>NESTED DICTIONARY<\/strong><\/h1>\n\n\n\n<p>A dictionary can contain another dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">students = {<br>    \"student1\": {\"name\": \"Ali\", \"age\": 20},<br>    \"student2\": {\"name\": \"Sara\", \"age\": 22}<br>}print(students[\"student1\"][\"name\"])<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTANT RULES<\/strong><\/h1>\n\n\n\n<p>\u2022 Keys must be unique<br>\u2022 Keys must be immutable (string, number, tuple)<br>\u2022 Values can be any data type<br>\u2022 Dictionaries are mutable<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHEN TO USE DICTIONARIES<\/strong><\/h1>\n\n\n\n<p>\u2022 When storing related data<br>\u2022 When mapping one value to another<br>\u2022 For structured data like records, JSON, APIs<br>\u2022 When fast key-based lookup is needed<\/p>\n\n\n\n<p>Understanding <strong>dictionaries<\/strong> is essential for handling structured and real-world data in Python programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON INTERMEDIATE (PYI) > Data Structures > Dictionaries<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773385448124\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":29,"template":"","class_list":["post-76","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Dictionaries - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python dictionaries: create, access, update, and delete key-value pairs, use methods, and handle nested structured data efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dictionaries - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python dictionaries: create, access, update, and delete key-value pairs, use methods, and handle nested structured data efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T07:34:36+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dictionaries\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dictionaries\\\/\",\"name\":\"Dictionaries - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T11:58:37+00:00\",\"dateModified\":\"2026-03-13T07:34:36+00:00\",\"description\":\"Learn Python dictionaries: create, access, update, and delete key-value pairs, use methods, and handle nested structured data efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dictionaries\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dictionaries\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dictionaries\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Data Structures > Dictionaries\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dictionaries - One Language. Endless Possibilities","description":"Learn Python dictionaries: create, access, update, and delete key-value pairs, use methods, and handle nested structured data efficiently.","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:\/\/gigz.pk\/python\/lesson\/dictionaries\/","og_locale":"en_US","og_type":"article","og_title":"Dictionaries - One Language. Endless Possibilities","og_description":"Learn Python dictionaries: create, access, update, and delete key-value pairs, use methods, and handle nested structured data efficiently.","og_url":"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T07:34:36+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/","url":"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/","name":"Dictionaries - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T11:58:37+00:00","dateModified":"2026-03-13T07:34:36+00:00","description":"Learn Python dictionaries: create, access, update, and delete key-value pairs, use methods, and handle nested structured data efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/dictionaries\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/dictionaries\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Data Structures > Dictionaries"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}