{"id":667,"date":"2024-07-30T09:34:36","date_gmt":"2024-07-30T04:34:36","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=667"},"modified":"2024-10-05T09:05:15","modified_gmt":"2024-10-05T04:05:15","slug":"python-dictionaries","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-dictionaries\/","title":{"rendered":"Python: Dictionaries"},"content":{"rendered":"\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>Many right implementations could be using lists or tuples, but none would be optimised for performance.<br>Python provides a data structure Dictionary for such cases. A dictionary in simple words is a data structure which stores the key-value pairs.<\/p>\n\n\n\n<p><strong>For example: if you want to store information about a student<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>student = { 'Name': 'Aaliyan', 'Class': 'AI', 'Program': 'PIAIC', 'Age': 42 }<\/strong><\/code><\/pre>\n\n\n\n<p>Please note that keys must be between single or double quotation if they are string type, every pair is separated from other pairs by a <strong>comma<\/strong> ( <strong>,<\/strong> ), and every pair of key value is separated by a <strong>colon<\/strong> ( <strong>:<\/strong> ).<\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Accessing information from Dictionary<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>student = { 'Name': 'Zaid', 'Class': 'AI', 'Program': 'PIAIC', 'Age': 42 }\nprint(student&#91;'Name'])<\/em><\/strong><\/code><\/pre>\n\n\n\n<p>If you try to access any key not present in the dictionary will raise an exception of type Key error. Also, beware of the fact that the Keys are case sensitive, hence the following statement will throw an exception of type Key error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>print(student&#91;'name']) <\/strong><em># Exception<\/em><\/code><\/pre>\n\n\n\n<p> because the name key does not exist in the student dictionary.<\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Dictionary adding keys<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>student = { 'Name': 'Zaid', 'Class': 'AI', 'Program': 'PIAIC', 'Age': 42 }<\/em><\/strong><\/code><\/pre>\n\n\n\n<p>In the dictionary <em><strong>student<\/strong><\/em>, we don&#8217;t have the <strong><em>FatherName<\/em><\/strong><em> <\/em>key what if now I want to assign a value to this key in the dictionary?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>student&#91;'FatherName'] = 'Bakar'<\/em><\/strong><\/code><\/pre>\n\n\n\n<p>When we are assigning a value in a key which does not exist, Python creates the key and assigns the value in the key. If a key is already present the value is overwritten.<\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Dictionary removing items<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>student = { 'Name': 'Zaid', 'Class': 'AI', 'Program': 'PIAIC', 'Age': 42 }<\/em><\/strong><\/code><\/pre>\n\n\n\n<p>There are cases when you must delete a key-value pair from the dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>del student&#91;'Program']\nprint(student)<\/em><\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Dictionary iterating items<\/h3>\n\n\n\n<p>Python provides 3 methods over dictionary objects to iterate over dictionary values, keys and key-value pair<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>student = { 'Name': 'Zaid', 'Class': 'AI', 'Program': 'PIAIC', 'Age': 42}<\/em><\/strong>\n<strong><em>\nfor value in student.values():\n    print(value)<\/em><\/strong>\n<strong><em>\nfor key in student.keys():\n   print(key)<\/em><\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Dictionary; what you can store<\/h3>\n\n\n\n<p>Any data structure we have studied so far we can store as value in the dictionary.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The dictionary can contain a list<\/li>\n\n\n\n<li>Tuple<\/li>\n\n\n\n<li>Any type of primitive or user-defined (in future lectures we will study them as classes)<\/li>\n\n\n\n<li>Dictionary, a dictionary can store another dictionary as its value<\/li>\n\n\n\n<li>Or a combination of these<\/li>\n<\/ul>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"kt-adv-heading667_144a53-6a wp-block-kadence-advancedheading has-base-2-color has-text-color\" data-kb-block=\"kb-adv-heading667_144a53-6a\">List of Dictionaries<\/h2>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Creating a list of dictionaries<\/h3>\n\n\n\n<p>To mimic a database we can create a list of dictionaries, where the list will be an in-memory database and each dictionary object will represent a unique record. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>students&#91;]\nstudents.append({ 'Name': 'Ali', 'Class': 'AI', 'Campus': 'PIAIC Campus 1'})\nstudents.append({ 'Name': 'Jinnah', 'Class': 'AI', 'Campus': 'PIAIC Campus 2'})<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Accessing info from a list of dictionaries<\/h3>\n\n\n\n<p>We are already familiar with the list of elements accessed by their index. Hence, to access any dictionary object you need its index to access it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>students = &#91;]<\/strong>\n<strong>students.append({ 'Name': 'Ali', 'Class': 'AI', 'Campus': 'PIAIC Campus 1'})<\/strong>\n<strong>students.append({ 'Name': 'Jinah', 'Class': 'AI', 'Campus': 'PIAIC Campus 2'})<\/strong>\n\n<strong>for student in students:<\/strong>\n     <strong>print(student)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">a dictionary that holds a list<\/h3>\n\n\n\n<p>As we have studied to access the value of a dictionary we provide a key name inside square brackets, in this case, the value is a list so an extra pair of square brackets is used to access the list element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>employee = { 'Name': 'Shams', 'ChildrenNames': &#91;'Humairah', 'Abdul Rehman']}\nprint(employee&#91;'ChildrenNames']&#91;0])\nprint(employee&#91;'ChildrenNames']&#91;1])<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">a dictionary that holds a dictionary<\/h3>\n\n\n\n<p>In this snippet, we created a dictionary inside the dictionary, and the child&#8217;s name is used as a key. In the first print statement we can observe that the first pair of square brackets with employee returns a dictionary to access the elements of that dictionary we provide another pair with a key.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>employee = { 'Name': 'Shams', 'Children': {'Humairah': {'Age': 10, 'Class': '6th Standard' }, 'Abdul Rahman': {'Age': 8, 'Class': '4th Standard' }}}<\/strong>\n\n<strong>print(employee&#91;'Children']&#91;'Humairah'])<\/strong>\n<strong>print(employee&#91;'Children']&#91;'Abdul Rahman']&#91;'Age'])<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Many right implementations could be using lists or tuples, but none would be optimised for performance.Python provides a data structure Dictionary for such cases. A dictionary in simple words is a data structure which stores the key-value pairs. For example: if you want to store information about a student Please note that keys must be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-667","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":26,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":792,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/667\/revisions\/792"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}