{"id":5818,"date":"2022-04-02T15:06:59","date_gmt":"2022-04-02T13:06:59","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5818"},"modified":"2026-01-24T13:50:24","modified_gmt":"2026-01-24T12:50:24","slug":"dictionaries-in-python","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/04\/02\/dictionaries-in-python\/","title":{"rendered":"Dictionaries in Python"},"content":{"rendered":"<p>1. Dictionaries are unordered<strong>*<\/strong>, changeable (mutable), and indexed collections of data. (<strong>*<\/strong>In Python 3.6x dictionaries have become ordered by default.<\/p>\n<p><!--more--><\/p>\n<p>Each dictionary is a set of <i>key: value<\/i> pairs. You can create it by using the following syntax:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_dictionary = {\r\n  key1: value1,\r\n  key2: value2,\r\n  key3: value3,\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>2. If you want to access a dictionary item, you can do so by making a reference to its key inside a pair of square brackets (ex. 1) or by using the <code>get()<\/code> method (ex. 2):<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">pol_eng = {\r\n  \"kwiat\": \"flower\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n\r\nitem_1 = pol_eng[\"gleba\"] # ex. 1\r\nprint(item_1) # outputs: soil\r\n\r\nitem_2 = pol_eng.get(\"woda\")\r\nprint(item_2) # outputs: water<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"ace_line\">3. If you want to change the value associated with a specific key, you can do so by referring to the item&#8217;s key name in the following way:<\/div>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">pol_eng = {\r\n  \"zamek\": \"castle\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n\r\npol_eng[\"zamek\"] = \"lock\"\r\nitem = pol_eng[\"zamek\"]\r\nprint(item) # outputs: lock<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>4. To add or remove a key (and the associated value), use the following syntax:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">phonebook = {} # an empty dictionary\r\n\r\nphonebook[\"Adam\"] = 3456783958 # create\/add a key-value pair\r\nprint(phonebook) # outputs: {'Adam': 3456783958}\r\n\r\ndel phonebook[\"Adam\"]\r\nprint(phonebook) # outputs: {}<\/pre>\n<p>You can also insert an item to a dictionary by using the <code>update()<\/code> method, and remove the last element by using the <code>popitem()<\/code> method, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">pol_eng = {\"kwiat\": \"flower\"}\r\n\r\npol_eng.update({\"gleba\": \"soil\"})\r\nprint(pol_eng) # outputs: {'kwiat': 'flower', 'gleba': 'soil'}\r\n\r\npol_eng.popitem()\r\nprint(pol_eng) # outputs: {'kwiat': 'flower'}<\/pre>\n<p>&nbsp;<\/p>\n<p>5. Loop through a keys of the dictionary, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">pol_eng = {\r\n  \"zamek\": \"castle\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n\r\nfor k in pol_eng:\r\n  print(k)\r\n\r\n<\/pre>\n<p><code>zamek<\/code><br \/>\n<code>woda<\/code><br \/>\n<code>gleba<\/code><\/p>\n<p>or<\/p>\n<\/div>\n<\/div>\n<div>\n<pre class=\"lang:default decode:true \">pol_eng = { \r\n    \"zamek\": \"castle\", \r\n    \"woda\": \"water\", \r\n    \"gleba\": \"soil\" } \r\n\r\nfor k in pol_eng.keys(): \r\n    print(k)<\/pre>\n<p><code>zamek<\/code><br \/>\n<code>woda<\/code><br \/>\n<code>gleba<\/code><\/p>\n<\/div>\n<p>7.\u00a0 Loop through a dictionary&#8217;s values<\/p>\n<pre class=\"lang:default decode:true \">pol_eng = { \r\n    \"zamek\": \"castle\", \r\n    \"woda\": \"water\", \r\n    \"gleba\": \"soil\" } \r\n\r\nfor v in pol_eng.values(): \r\n    print(v)<\/pre>\n<p><code>castle<\/code><br \/>\n<code>water<\/code><br \/>\n<code>soli<\/code><\/p>\n<p>or<\/p>\n<pre class=\"lang:default decode:true\">pol_eng = { \r\n    \"zamek\": \"castle\", \r\n    \"woda\": \"water\", \r\n    \"gleba\": \"soil\" } \r\n\r\nfor i in pol_eng.keys():\r\n    print(pol_eng[i])\r\n<\/pre>\n<p><code>castle<\/code><br \/>\n<code>water<\/code><br \/>\n<code>soli<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>6. If you want to loop through a dictionary&#8217;s keys and values, you can use the <code>items()<\/code> method, e.g.:<\/p>\n<pre class=\"lang:default decode:true\">pol_eng = {\r\n  \"zamek\": \"castle\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n \r\nfor value in pol_eng.items():\r\n  print(value)<\/pre>\n<p><code>('zamek', 'castle') <\/code><br \/>\n<code>('woda', 'water') <\/code><br \/>\n<code>('gleba', 'soil')<\/code><\/p>\n<p>or<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">pol_eng = {\r\n  \"zamek\": \"castle\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n\r\nfor k, v in pol_eng.items():\r\n  print(k, \":\", v)<\/pre>\n<div class=\"\" data-panel=\"\" data-panel-group-id=\"8\" data-panel-id=\"11\" data-panel-size=\"89.0\">\n<div class=\"jss79 jss80 \">\n<p><code>zamek : castle <\/code><br \/>\n<code>woda : water <\/code><br \/>\n<code>gleba : soil<\/code><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<p>8. To check if a given key exists in a dictionary, you can use the <code>in<\/code> keyword:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">pol_eng = {\r\n  \"zamek\": \"castle\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n\r\nif \"zamek\" in pol_eng:\r\n  print(\"Yes\")\r\nelse:\r\n  print(\"No\")<\/pre>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>9. You can use the <code>del<\/code> keyword to remove a specific item, or delete a dictionary. To remove all the dictionary&#8217;s items, you need to use the <code>clear()<\/code> method:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">pol_eng = {\r\n  \"zamek\": \"castle\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n\r\nprint(len(pol_eng)) # outputs: 3\r\ndel pol_eng[\"zamek\"] # remove an item\r\nprint(len(pol_eng)) # outputs: 2\r\n\r\npol_eng.clear() # removes all the items\r\nprint(len(pol_eng)) # outputs: 0\r\n\r\ndel pol_eng # removes the dictionary<\/pre>\n<p>&nbsp;<\/p>\n<p>10. To copy a dictionary, use the <code>copy()<\/code> method:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">pol_eng = {\r\n  \"zamek\": \"castle\",\r\n  \"woda\": \"water\",\r\n  \"gleba\": \"soil\"\r\n  }\r\n\r\ncopy_dictionary = pol_eng.copy()<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 1<\/strong><\/p>\n<p>Write a program that will &#8220;glue&#8221; the two dictionaries (<code>d1<\/code> and <code>d2<\/code>) together and create a new one (<code>d3<\/code>).<\/p>\n<pre class=\"lang:default decode:true \">d1 = {'Adam Smith': 'A', 'Judy Paxton': 'B+'} \r\nd2 = {'Mary Louis': 'A', 'Patrick White': 'C'} \r\nd3 = {} \r\n\r\nfor item in (d1, d2): \r\n  # Write your code here. print(d3)<\/pre>\n<div align=\"right\"><\/div>\n<div class=\"small-12 large-6 columns\">\n<p>Sample solution:<\/p>\n<pre class=\"lang:default decode:true \">d1 = {'Adam Smith': 'A', 'Judy Paxton': 'B+'} \r\nd2 = {'Mary Louis': 'A', 'Patrick White': 'C'} \r\nd3 = {} \r\n\r\nfor item in (d1, d2): \r\n  d3.update(item) \r\n\r\nprint(d3)<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 2<\/strong><\/p>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<p>Write a program that will convert the <code>my_list<\/code> list to a tuple.<\/p>\n<pre class=\"lang:default decode:true \">my_list = [\"car\", \"Ford\", \"flower\", \"Tulip\"] \r\nt = # Write your code here. \r\n\r\nprint(t)<\/pre>\n<p>Sample solution:<\/p>\n<\/div>\n<pre class=\"lang:default decode:true \">my_list = [\"car\", \"Ford\", \"flower\", \"Tulip\"] \r\nt = tuple(my_list) \r\n\r\nprint(t)<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 3<\/strong><\/p>\n<p>Write a program that will convert the <code>colors<\/code> tuple to a dictionary.<\/p>\n<pre class=\"lang:default decode:true \">colors = ((\"green\", \"#008000\"), (\"blue\", \"#0000FF\")) \r\n# Write your code here. \r\n\r\nprint(colors_dictionary)<\/pre>\n<p>Sample solution:<\/p>\n<pre class=\"lang:default decode:true \">colors = ((\"green\", \"#008000\"), (\"blue\", \"#0000FF\")) \r\ncolors_dictionary = dict(colors) \r\n\r\nprint(colors_dictionary)<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 4<\/strong><\/p>\n<p>What will happen when you run the following code?<\/p>\n<pre class=\"lang:default decode:true \">my_dictionary = {\"A\": 1, \"B\": 2} \r\ncopy_my_dictionary = my_dictionary.copy() \r\n\r\nmy_dictionary.clear() \r\nprint(copy_my_dictionary)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol7\">The program will print <code>{'A': 1, 'B': 2}<\/code> to the screen.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 5<\/strong><\/p>\n<p>What is the output of the following program?<\/p>\n<pre class=\"lang:default decode:true \">colors = { \r\n  \"white\": (255, 255, 255), \r\n  \"grey\": (128, 128, 128), \r\n  \"red\": (255, 0, 0), \r\n  \"green\": (0, 128, 0) \r\n  } \r\n\r\nfor col, rgb in colors.items(): \r\n  print(col, \":\", rgb)<\/pre>\n<p>&nbsp;<\/p>\n<p>white : (255, 255, 255)<\/p>\n<p>grey : (128, 128, 128)<\/p>\n<p>red : (255, 0, 0)<\/p>\n<p>green : (0, 128, 0)<\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 6<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"lang:default decode:true \">d = {'c': 3, 'a': 1, 'a': 5, 'a': 2}\r\nprint(d['a'])\r\n<\/pre>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>2<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>Dictionaries do not allow multiple elements with the same key. However, Python doesn&#8217;t raise an exception but stores in the dictionary only the <strong>last<\/strong> <strong>element<\/strong> among those with <strong>equal key<\/strong>:<code><strong>d = {'c': 3, 'a': 1, 'a': 5, 'a': 2}<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>d = {'c': 3, 'a': 2}<\/strong><\/code>.<code><strong>print(d['a'])<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>2<\/strong><\/code> .<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div align=\"right\"><\/div>\n<div class=\"ace_line\"><\/div>\n<div>Ex. 7<\/div>\n<div>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Which of the following statements <strong>correctly<\/strong> creates a <strong>list<\/strong>?<\/p>\n<p>(Select two answers)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>lst3 = list('a', 'b')<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>lst2 = 1, 2, 3<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>lst4 = list(('a', 'b'))<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>lst1 = [1, 2, 2, '3', [4, 5]]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>lst4 = list(('a', 'b'))<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>lst1 = [1, 2, 2, '3', [4, 5]]<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<p><strong>Try the code in your favorite Python IDE<\/strong><\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<pre class=\"lang:default decode:true\">lst1 = [1, 2, 2, '3', [4, 5]]\r\nlst2 = 1, 2, 3\r\n# lst3 = list('a', 'b') # TypeError\r\nlst4 = list(('a', 'b'))\r\nprint(lst1)\r\nprint(lst2)\r\nprint(lst4)<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The statement <code><strong>lst1 = [1, 2, 2, '3', [4, 5]]<\/strong><\/code> <strong>correctly<\/strong> creates the list <code><strong>lst1<\/strong><\/code>, with elements of various types (even duplicates) enclosed in square brackets.<\/li>\n<li>The instruction l<code><strong>st2 = 1, 2, 3<\/strong><\/code> does not create a list but a <strong>tuple<\/strong>.<\/li>\n<li><code><strong>lst3 = list('a', 'b')<\/strong><\/code> is <strong>incorrect<\/strong> because the <code><strong>list()<\/strong><\/code> function accepts at most <strong>one argument<\/strong> while it receives <strong>two<\/strong>. It raises a <code><strong>TypeError<\/strong><\/code> exception.<\/li>\n<li>The statement <code><strong>lst4 = list(('a', 'b'))<\/strong><\/code> still uses the <code><strong>list()<\/strong><\/code><strong>constructor<\/strong> to create the list. In this case, however, the answer is <strong>correct<\/strong> because it only receives <strong>one argument<\/strong>, a <strong>tuple<\/strong>, which it converts to a list.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">Ex. 8<\/div>\n<div data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Given the code snippet below, what statement should you insert in place of the <strong>comment line<\/strong> to get the following output:<\/p>\n<p><code>{1: 'one', 2: 'two', 3: 'three', 4: 'four'}<\/code>?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">d1 = {1: 'one', 2: 'two'}\r\nd2 = {3: 'three', 4: 'four'}\r\n\u00a0\r\n# Insert code here\r\n\u00a0\r\nprint(d1)<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d1 = d1 + d2<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d1.insert(d2)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d1.update(d2)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d1.append(d2)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<p><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>d1.update(d2)<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul>\n<li>The statement <code><strong>d1.update(d2)<\/strong><\/code> is the <strong>correct<\/strong> answer. The parameter of the <strong><code>update()<\/code><\/strong> method can be a dictionary or an iterable object with <strong>key-value <\/strong>pairs.<\/li>\n<\/ul>\n<ul>\n<li>All other statements are <strong>wrong<\/strong>.: dictionaries do not support the <code><strong>append()<\/strong><\/code> and <code><strong>insert()<\/strong><\/code> methods and the <code><strong>+<\/strong><\/code> operator.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 9<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Which of the following <strong>dictionaries<\/strong> are created <strong>correctly<\/strong>?<\/p>\n<p>(Select two answers)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d2 = {[1, 2]: 1, [3, 4]: 3}<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d4 = {'Mary': 30, 'Robert': 28}<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d3 = {'y': 1, 'x': 10, 'x': 100}<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d1 = {}<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>d4 = {'Mary': 30, 'Robert': 28}<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>d1 = {}<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The statement <code><strong>d1 = {}<\/strong><\/code>\u00a0 is <strong>correct<\/strong>. It creates an <strong>empty<\/strong> dictionary.<\/li>\n<\/ul>\n<ul>\n<li>The statement <code><strong>d4 = {'Mary': 30, 'Robert': 28}<\/strong><\/code> is also <strong>ok<\/strong>. Each item is correctly composed of a key and its associated value.<\/li>\n<\/ul>\n<ul>\n<li>The statement <code><strong>d2 = {[1, 2]: 1, [3, 4]: 3}<\/strong><\/code> is <strong>wrong<\/strong>. It raises a <code><strong>TypeError<\/strong><\/code> exception. Lists are not allowed as dictionary keys, because they are mutable objects. Dictionary keys can only be of immutable types: integer, string, tuple, etc.<\/li>\n<\/ul>\n<ul>\n<li>The statement <code><strong>d3 = {'y': 1, 'x': 10, 'x': 100}<\/strong><\/code> is also <strong>incorrect<\/strong>, because the key <code><strong>'x'<\/strong><\/code> is duplicated. The dictionary is created, but it has only the last element among those with the duplicate key:<code><strong>d2 = {'y': 1, 'x': 10, 'x': 100} <\/strong><\/code><strong>-&gt; <\/strong><code><strong>d2 = {'y': 1, 'x': 100}<\/strong><\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 10<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true\">d = {3: 'a', 5: 'b', 1: 'c', 7: 'd'}\r\nprint(sorted(d))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>{1: 'c', 3: 'a', 5: 'b', 7: 'd'}<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[1, 3, 5, 7]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>(1, 3, 5, 7)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>1, 3, 5, 7<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<p><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>[1, 3, 5, 7]<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul>\n<li>The <code><strong>sorted()<\/strong><\/code> function returns a sorted list of an iterable object. In our example, it returns a list that contains only the keys of dictionary <code><strong>d<\/strong><\/code>, sorted numerically. The sorting is <strong>ascending<\/strong> by default.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 11<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Consider the following dictionary:<\/p>\n<pre class=\"lang:default decode:true\">d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}.<\/pre>\n<p>Which of the following statements <strong>removes<\/strong> the element with the key <code><strong>'c'<\/strong><\/code> from the <code><strong>d<\/strong><\/code> dictionary?<\/p>\n<p>(Select two answers)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d.remove('c')<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d.del('c')<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>d.pop('c')<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>del d['c']<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>d.pop('c')<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>del d['c']<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul>\n<li>Statements <code><strong>del d['c']<\/strong><\/code> and <code><strong>d.pop('c')<\/strong><\/code> are the <strong>correct<\/strong> answers. The <code><strong>del<\/strong><\/code> statement and the <code><strong>pop()<\/strong><\/code> method remove the element associated with the key passed as the argument.<\/li>\n<li>Statement <code><strong>d.del('c')<\/strong><\/code> is <strong>wrong<\/strong> and generates a <code><strong>SyntaxError<\/strong><\/code> exception because <code><strong>del<\/strong><\/code> is not a method.<\/li>\n<li>Statement <code><strong>d.remove('c')<\/strong><\/code> is <strong>not ok<\/strong>: <code><strong>remove()<\/strong><\/code> is not a dictionary method but a <strong>list<\/strong> <strong>method <\/strong>and it generates an <code><strong>AttributeError<\/strong><\/code> exception.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 12<\/p>\n<p>What is the expected output of the following snippet ?<\/p>\n<pre class=\"lang:default decode:true \">    l={}\r\n    for i in 'Skywalker':\r\n        if i in 'Vader':\r\n           l[ord(i)]=i\r\n    else:\r\n        l[ord('d')]='d' \r\n     \r\n    for j in l.keys():\r\n        print(l[j], end=' ')<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>The <code>l<\/code> variable is an empty dictionary initially.<\/p>\n<p>The <code>for <\/code>loop populates the dictionary with the characters from the string &#8216;Skywalker&#8221; that are in the string &#8216;Vader&#8217; (i.e. a, e and r) : those are the values of the dictionary while the corresponding keys are the Unicode code of the corresponding character (the <code>ord()<\/code> function returns the Unicode code of a character).<\/p>\n<p>In a <strong>for-else<\/strong> loop, the <code>else:<\/code> clause is executed when the code below the <code>for<\/code> loop has completed successfully &#8211; in this case the code in the <code>else:<\/code> clause adds the following key:value pair to the dictionary : <code>100:'d'<\/code>.<\/p>\n<p>So by the end of the first for-else loop, the <code>l<\/code> dictionary is as follow:<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><code><span class=\"pun\">{<\/span><span class=\"lit\">97<\/span><span class=\"pun\">:<\/span> <span class=\"str\">'a'<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">101<\/span><span class=\"pun\">:<\/span> <span class=\"str\">'e'<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">114<\/span><span class=\"pun\">:<\/span> <span class=\"str\">'r'<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">100<\/span><span class=\"pun\">:<\/span> <span class=\"str\">'d'<\/span><span class=\"pun\">}<\/span><\/code><\/p>\n<\/div>\n<\/div>\n<p>The last <code>for <\/code>loop, simply print each value from the <code>l<\/code> dictionary using the <code>end <\/code>parameter of the <code>print()<\/code> function to have the characters nicely printed on one line, separated by a space.<\/p>\n<p>Resulting output is :<\/p>\n<p><code>a e r d <\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 13<\/p>\n<p>What is the output of the following snippet ?<\/p>\n<pre class=\"lang:default decode:true \">    my_list = {'a': ord('a'), 'b': ord('b'), 'c': ord('c'), 'd': ord('d') }\r\n    list=[]\r\n    for i in sorted(my_list.keys()):\r\n        list.insert(-1,my_list[i])\r\n     \r\n    print(list)<\/pre>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><code><span class=\"pun\">A: [<\/span><span class=\"lit\">97<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">98<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">99<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">100<\/span><span class=\"pun\">]<\/span><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><code><span class=\"pun\">B: [<\/span><span class=\"lit\">100<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">99<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">98<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">97<\/span><span class=\"pun\">]<\/span><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><code><span class=\"pun\">C: [<\/span><span class=\"lit\">98<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">99<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">100<\/span><span class=\"pun\">,<\/span> <span class=\"lit\">97<\/span><span class=\"pun\">]<\/span><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><code><span class=\"pun\">D: [<\/span><span class=\"str\">'a'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'b'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'c'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'d'<\/span><span class=\"pun\">]<\/span><\/code><\/p>\n<p>Explanation:<\/p>\n<p><code>my_list<\/code> is a dictionary (the poorly chosen name was selected to confuse you).<\/p>\n<p>The <code>ord()<\/code> function returns the Unicode code from a given character. So <code>my_list<\/code> is a dictionary that looks like this :<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><code><span class=\"pun\">{<\/span><span class=\"str\">'a'<\/span><span class=\"pun\">:<\/span> <span class=\"lit\">97<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'b'<\/span><span class=\"pun\">:<\/span> <span class=\"lit\">98<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'c'<\/span><span class=\"pun\">:<\/span> <span class=\"lit\">99<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'d'<\/span><span class=\"pun\">:<\/span> <span class=\"lit\">100<\/span><span class=\"pun\">}<\/span><\/code><\/p>\n<\/div>\n<\/div>\n<p><code>my_list.keys()<\/code> will return a view object that displays the list of all the keys of the dictionary (in this case : <code>dict_keys(['a', 'b', 'c', 'd')]<\/code>).<\/p>\n<p>The <code>for <\/code>loop will iterate over each of those keys and <code>my_list[i] <\/code>will be the value from the dictionary for the corresponding key ( for example : <code>my_list['a'] <\/code>&#8211;&gt;<code> 97<\/code>). So the <code>for <\/code>loop will simply insert each <strong>value <\/strong>from the dictionary in the list <code>list<\/code> using the <code>insert()<\/code> method.<\/p>\n<p>The\u00a0 <code>insert()<\/code> method inserts a given element at a given index in a list. The syntax is :<\/p>\n<p><code><em>list_name.insert(index, element)<\/em><\/code>.<\/p>\n<p>In this case, index = -1 ; pay particular attention to the behavior of the <code>insert()<\/code> method in this case &#8211; see print out below for details.<\/p>\n<p>Try it yourself:<\/p>\n<pre class=\"lang:default decode:true \">    my_list = {'a': ord('a'), 'b': ord('b'), 'c': ord('c'), 'd': ord('d') }\r\n    print(my_list)\r\n    list=[]\r\n    for i in sorted(my_list.keys()):\r\n        print(my_list[i])\r\n        list.insert(-1,my_list[i])\r\n        print(list)\r\n     \r\n    print(list)\r\n     \r\n    # Following output will be printed :\r\n    #{'a': 97, 'b': 98, 'c': 99, 'd': 100}\r\n    #97\r\n    #[97]\r\n    #98\r\n    #[98, 97]\r\n    #99\r\n    #[98, 99, 97]\r\n    #100\r\n    #[98, 99, 100, 97]\r\n    #[98, 99, 100, 97]<\/pre>\n<p>C.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 14<\/p>\n<p>What code would you insert instead of the comment to obtain the expected output ?<\/p>\n<p><em>Expected output :<\/em><\/p>\n<\/div>\n<\/div>\n<pre class=\"lang:default decode:true \">    Luke\r\n    Han\r\n    Leia<\/pre>\n<p><em>Code :<\/em><\/p>\n<pre class=\"lang:default decode:true \">    my_dict = {}\r\n    my_list = ['Luke', 'Han', 'Leia', 'Ben']\r\n     \r\n    for i in range(len(my_list)-1):\r\n        my_dict[i] = (my_list[i], )\r\n     \r\n    for i in sorted(my_dict.keys()):\r\n        j = my_dict[i]\r\n        # Insert your code here<\/pre>\n<p><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">A. <\/span><code>print(j[0])<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">B. <\/span><code>print(j)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>C. <code>print(j[i])<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>D. <code>print(j['0'])<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>&nbsp;<\/p>\n<p><em>Explanation:<\/em><\/p>\n<p>The code mixes lists, tuples and dictionaries.<\/p>\n<p>The first <code>for<\/code> loop will update the initially empty dictionary <code>my_dict <\/code>as follow:<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><code><span class=\"pun\">{<\/span><span class=\"lit\">0<\/span><span class=\"pun\">:<\/span> <span class=\"pun\">(<\/span><span class=\"str\">'Luke'<\/span><span class=\"pun\">,),<\/span> <span class=\"lit\">1<\/span><span class=\"pun\">:<\/span> <span class=\"pun\">(<\/span><span class=\"str\">'Han'<\/span><span class=\"pun\">,),<\/span> <span class=\"lit\">2<\/span><span class=\"pun\">:<\/span> <span class=\"pun\">(<\/span><span class=\"str\">'Leia'<\/span><span class=\"pun\">,)}<\/span><\/code><\/p>\n<\/div>\n<\/div>\n<p><code>my_dict <\/code>is a dictionary with keys being integer <code>0<\/code>, <code>1<\/code>, <code>2<\/code> and corresponding values are <strong>tuples : <\/strong><code>('Luke',)<\/code> , <code>('Han',)<\/code>, <code>('Leia',)<\/code>.<\/p>\n<p>The second <code>for\u00a0 <\/code>loop will assign variable <code>j<\/code> with the values of the dictionary, which are <strong>tuples<\/strong>. So <code>j<\/code> is a tuple too.<\/p>\n<p>To print the expected output, you need to access the first item from the tuple , &#8211; i.e. <code>j[0]<\/code><\/p>\n<p>Try it yourself:<\/p>\n<pre class=\"lang:default decode:true \">    my_dict = {}\r\n    my_list = ['Luke', 'Han', 'Leia', 'Ben']\r\n     \r\n    for i in range(len(my_list)-1):\r\n        my_dict[i] = (my_list[i], )\r\n     \r\n    print(my_dict)     # {0: ('Luke',), 1: ('Han',), 2: ('Leia',)}\r\n     \r\n    for i in sorted(my_dict.keys()):\r\n        j = my_dict[i]\r\n        print(j[0])    # Luke -&gt; Han -&gt; Leia\r\n        #print(j[i])   # Luke -&gt; exception : IndexError: tuple index out of range\r\n        #print(j)      # ('Luke',) -&gt; ('Han',) -&gt; ('Leia',)\r\n        #print(j['0']) # exception : TypeError: tuple indices must be integers or slices, not str<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Dictionaries are unordered*, changeable (mutable), and indexed collections of data. (*In Python 3.6x dictionaries have become ordered by default.<\/p>\n","protected":false},"author":2,"featured_media":5819,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5818"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5818"}],"version-history":[{"count":21,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5818\/revisions"}],"predecessor-version":[{"id":6290,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5818\/revisions\/6290"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/5819"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5818"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}