{"id":236,"date":"2023-06-12T20:42:49","date_gmt":"2023-06-12T20:42:49","guid":{"rendered":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?page_id=236"},"modified":"2023-06-12T20:43:40","modified_gmt":"2023-06-12T20:43:40","slug":"dictionaries","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/","title":{"rendered":"Dictionaries"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Introduction<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Introduction to dictionaries<\/h3>\n<p>Dictionaries are a fundamental<strong> data structure<\/strong> in programming that allow you to store and retrieve data in a structured and organized manner. They are also known as associative arrays, hash maps, or hash tables in some programming languages.<\/p>\n<p>In simple terms, a dictionary is like a real-life dictionary or a phone book. It consists of a collection of<strong> key-value pairs<\/strong>, where each key is unique and associated with a corresponding value. Think of the keys as the words in a dictionary, and the values as their respective definitions.<\/p>\n<p>Dictionaries provide efficient and fast look-up operations by using a technique called <strong>hashing<\/strong>. This means that instead of searching through the entire collection of data, the dictionary uses a unique hash code generated from the key to quickly locate the associated value.<\/p>\n<p>One of the key benefits of dictionaries is their versatility in storing different types of data. The keys can be of any <strong>immutable type<\/strong>, such as strings, numbers, or tuples, while the values can be of any data type, including numbers, strings, lists, or even other dictionaries.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Example 1<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Example 1 &#8211; Name and address adder<br \/>\n<!-- HTML generated using hilite.me --><\/h3>\n<p>This simple program asks the user to enter names and ages and them adds them to a dictionary.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">name_age_dict <span style=\"color: #333333;\">=<\/span> {}\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">while<\/span> <span style=\"color: #007020;\">True<\/span>:\r\n    name <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">input<\/span>(<span style=\"background-color: #fff0f0;\">\"Enter a name (or 'q' to quit): \"<\/span>)\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> name <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">'q'<\/span>:\r\n        <span style=\"color: #008800; font-weight: bold;\">break<\/span>\r\n\r\n    age <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">input<\/span>(<span style=\"background-color: #fff0f0;\">\"Enter the age for {}: \"<\/span><span style=\"color: #333333;\">.<\/span>format(name))\r\n    age <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">int<\/span>(age)  <span style=\"color: #888888;\"># Convert age to an integer<\/span>\r\n\r\n    name_age_dict[name] <span style=\"color: #333333;\">=<\/span> age\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Names and ages:\"<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> name, age <span style=\"color: #000000; font-weight: bold;\">in<\/span> name_age_dict<span style=\"color: #333333;\">.<\/span>items():\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"{}: {} years old\"<\/span><span style=\"color: #333333;\">.<\/span>format(name, age))\r\n<\/pre>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Example 2<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Example 2 &#8211; Letter counter<\/h3>\n<p>In this example we write a simple function that keeps track of the numbers of times letters are contained within a string.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">count_letter_occurrences<\/span>(string):\r\n    letter_count <span style=\"color: #333333;\">=<\/span> {}\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> letter <span style=\"color: #000000; font-weight: bold;\">in<\/span> string:\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span> letter<span style=\"color: #333333;\">.<\/span>isalpha():\r\n            letter <span style=\"color: #333333;\">=<\/span> letter<span style=\"color: #333333;\">.<\/span>lower()  <span style=\"color: #888888;\"># Convert letter to lowercase for case-insensitive counting<\/span>\r\n\r\n            <span style=\"color: #008800; font-weight: bold;\">if<\/span> letter <span style=\"color: #000000; font-weight: bold;\">in<\/span> letter_count:\r\n                letter_count[letter] <span style=\"color: #333333;\">+=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n            <span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n                letter_count[letter] <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> letter_count\r\n\r\n<span style=\"color: #888888;\"># Example usage<\/span>\r\nsentence <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Hello, world!\"<\/span>\r\noccurrences <span style=\"color: #333333;\">=<\/span> count_letter_occurrences(sentence)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> letter, count <span style=\"color: #000000; font-weight: bold;\">in<\/span> occurrences<span style=\"color: #333333;\">.<\/span>items():\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(f<span style=\"background-color: #fff0f0;\">\"Letter '{letter}' occurs {count} times.\"<\/span>)\r\n<\/pre>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Uses of dictionaries<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Dictionaries programming challenges<\/h3>\n<ol>\n<li>Word Counter: Write a program that counts the occurrences of words in a given sentence or text. Use a dictionary to store the words as keys and their corresponding counts as values. Iterate through the text, update the counts in the dictionary, and display the results.<\/li>\n<li>Contact Book: Create a program that allows users to store and retrieve contact information. Use a dictionary to represent each contact, where the keys are names and the values are phone numbers or email addresses. Implement features like adding new contacts, searching for a contact by name, and displaying the entire contact book.<\/li>\n<li>Weather App: Build a program that retrieves weather data for different cities. Use a dictionary to map city names to their corresponding weather information, such as temperature, humidity, or weather conditions. Allow users to enter a city name, look up the weather data in the dictionary, and display it.<\/li>\n<li>Quiz Game: Develop a program that presents a quiz to the user. Store the quiz questions as keys in a dictionary, with the corresponding answers as the values. Randomly select questions from the dictionary and prompt the user for answers. Keep track of their score by comparing their answers with the values in the dictionary.<\/li>\n<li>Student Grade Calculator: Design a program that calculates and displays students&#8217; grades based on their scores. Use a dictionary to map students&#8217; names to their scores. Allow the user to enter the scores for each student, calculate their grades using predefined criteria, and display the final results with the corresponding grades.<\/li>\n<\/ol>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to dictionaries Dictionaries are a fundamental data structure in programming that allow you to store and retrieve data in a structured and organized manner. They are also known as associative arrays, hash maps, or hash tables in some programming languages. In simple terms, a dictionary is like a real-life dictionary or a phone book.&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Dictionaries<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Dictionaries - Edexcel iGCSE Computer Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dictionaries - Edexcel iGCSE Computer Science\" \/>\n<meta property=\"og:description\" content=\"Introduction to dictionaries Dictionaries are a fundamental data structure in programming that allow you to store and retrieve data in a structured and organized manner. They are also known as associative arrays, hash maps, or hash tables in some programming languages. In simple terms, a dictionary is like a real-life dictionary or a phone book.&hellip;&nbsp;Read More &raquo;Dictionaries\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/\" \/>\n<meta property=\"og:site_name\" content=\"Edexcel iGCSE Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-12T20:43:40+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/\",\"url\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/\",\"name\":\"Dictionaries - Edexcel iGCSE Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website\"},\"datePublished\":\"2023-06-12T20:42:49+00:00\",\"dateModified\":\"2023-06-12T20:43:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dictionaries\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website\",\"url\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/\",\"name\":\"Edexcel iGCSE Computer Science\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dictionaries - Edexcel iGCSE Computer Science","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:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/","og_locale":"en_GB","og_type":"article","og_title":"Dictionaries - Edexcel iGCSE Computer Science","og_description":"Introduction to dictionaries Dictionaries are a fundamental data structure in programming that allow you to store and retrieve data in a structured and organized manner. They are also known as associative arrays, hash maps, or hash tables in some programming languages. In simple terms, a dictionary is like a real-life dictionary or a phone book.&hellip;&nbsp;Read More &raquo;Dictionaries","og_url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/","og_site_name":"Edexcel iGCSE Computer Science","article_modified_time":"2023-06-12T20:43:40+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/","url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/","name":"Dictionaries - Edexcel iGCSE Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website"},"datePublished":"2023-06-12T20:42:49+00:00","dateModified":"2023-06-12T20:43:40+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/dictionaries\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/"},{"@type":"ListItem","position":2,"name":"Dictionaries"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website","url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/","name":"Edexcel iGCSE Computer Science","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Introduction to dictionaries Dictionaries are a fundamental data structure in programming that allow you to store and retrieve data in a structured and organized manner. They are also known as associative arrays, hash maps, or hash tables in some programming languages. In simple terms, a dictionary is like a real-life dictionary or a phone book.&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/236"}],"collection":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":2,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/236\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/236\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/media?parent=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}