{"id":85,"date":"2020-01-12T23:14:54","date_gmt":"2020-01-12T23:14:54","guid":{"rendered":"https:\/\/reghif.co.uk\/teach\/?p=85"},"modified":"2020-01-12T23:14:54","modified_gmt":"2020-01-12T23:14:54","slug":"lists-and-dictionaries","status":"publish","type":"post","link":"https:\/\/reghif.co.uk\/teach\/2020\/01\/12\/lists-and-dictionaries\/","title":{"rendered":"Lists and dictionaries"},"content":{"rendered":"\n<p>If you&#8217;re in Year 11 or above you&#8217;ll know what lists are&#8230;or you should have done Computing. Your loss. Or you did do Computing and weren&#8217;t listening. Your loss.<\/p>\n\n\n\n<p>So a list you can create like so:<\/p>\n\n\n\n<p><code>myList = [4, 8, 12]<\/code><\/p>\n\n\n\n<p>Nice and easy. You can then access the first item of the list like so:<\/p>\n\n\n\n<p><code>print(myList[0])<\/code><\/p>\n\n\n\n<p>So each item is numbered from 0. No problem. If you remove item 0, then the item in the next slot becomes item 0.<\/p>\n\n\n\n<p><code>print(myList[0])<\/code><br><code># Prints 4<br>myList.pop(0)<br># Removes the first item of the list<br>print(myList[0])<br># Prints 8<\/code><\/p>\n\n\n\n<p>You can see that a list has a definite order. In the original list 4 is the first item and 12 is the last item.<\/p>\n\n\n\n<p>Dictionaries don&#8217;t give a ____ about order. They are agents of chaos!!! Or hash tables. You should have covered hash tables in year 12. If you haven&#8217;t covered hash tables, they&#8217;re probably not what you&#8217;re thinking they are, but we can still move on.<\/p>\n\n\n\n<p>You can create a dictionary with curly brackets instead, but they need to be a pair of items.<\/p>\n\n\n\n<p><code>myDict = {0: 4, 1: 8, 2: 12}<\/code><\/p>\n\n\n\n<p>So you can see that item 0 has been assigned the value of 4. That is it&#8217;s value, it will not change (unless you explicitly change it).<\/p>\n\n\n\n<p><code>print(myDict[0])<br># Prints 4<br>del myDict[0]<br># Removes first element<br>print(myDict[0])<br># Key error - there is no myDict[0], you just deleted it.<br>print(myDict[1])<br># This would print 8, provided you hadn't already broken the code and hit a runtime error in the line before.<\/code><\/p>\n\n\n\n<p>The reasoning of this is that the dictionary is using a hash table to store the data, and it&#8217;s therefore got no intrinsic order. Whereas a list is an ordered list of one item followed by another. Different tools for different uses.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re in Year 11 or above you&#8217;ll know what lists are&#8230;or you should have done Computing. Your loss. Or you did do Computing and weren&#8217;t listening. Your loss. So a list you can create like so: myList = [4, 8, 12] Nice and easy. You can then access the first item of the list &#8230;.&nbsp;&nbsp;<a class=\" special\" href=\"https:\/\/reghif.co.uk\/teach\/2020\/01\/12\/lists-and-dictionaries\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[8,10],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-a-level","tag-a-level","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":1,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/posts\/85\/revisions\/86"}],"wp:attachment":[{"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reghif.co.uk\/teach\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}