{"id":1130,"date":"2024-02-24T10:50:23","date_gmt":"2024-02-24T10:50:23","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=1130"},"modified":"2024-02-24T10:50:24","modified_gmt":"2024-02-24T10:50:24","slug":"python-dictionaries","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/24\/python-dictionaries\/","title":{"rendered":"Python\u00a0Dictionaries"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>thisdict = {<br>&nbsp;&nbsp;\"brand\":&nbsp;\"Ford\",<br>&nbsp;&nbsp;\"model\":&nbsp;\"Mustang\",<br>&nbsp;&nbsp;\"year\":&nbsp;1964<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Dictionary<\/h2>\n\n\n\n<p>Dictionaries are used to store data values in key:value pairs.<\/p>\n\n\n\n<p>A dictionary is a collection which is ordered*, changeable and do not allow duplicates.<\/p>\n\n\n\n<p>As of Python version 3.7, dictionaries are&nbsp;<em>ordered<\/em>. In Python 3.6 and earlier, dictionaries are&nbsp;<em>unordered<\/em>.<\/p>\n\n\n\n<p>Dictionaries are written with curly brackets, and have keys and values:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Create and print a dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisdict =\u00a0{<br>\u00a0\u00a0\"brand\":\u00a0\"Ford\",<br>\u00a0\u00a0\"model\":\u00a0\"Mustang\",<br>\u00a0\u00a0\"year\":\u00a01964<br>}<br>print(thisdict)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Dictionary Items<\/h2>\n\n\n\n<p>Dictionary items are ordered, changeable, and do not allow duplicates.<\/p>\n\n\n\n<p>Dictionary items are presented in key:value pairs, and can be referred to by using the key name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Print the &#8220;brand&#8221; value of the dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisdict =\u00a0{<br>\u00a0\u00a0\"brand\":\u00a0\"Ford\",<br>\u00a0\u00a0\"model\":\u00a0\"Mustang\",<br>\u00a0\u00a0\"year\":\u00a01964<br>}<br>print(thisdict&#91;\"brand\"])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Ordered or Unordered?<\/h2>\n\n\n\n<p>As of Python version 3.7, dictionaries are&nbsp;<em>ordered<\/em>. In Python 3.6 and earlier, dictionaries are&nbsp;<em>unordered<\/em>.<\/p>\n\n\n\n<p>When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.<\/p>\n\n\n\n<p>Unordered means that the items do not have a defined order, you cannot refer to an item by using an index.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Changeable<\/h2>\n\n\n\n<p>Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Duplicates Not Allowed<\/h2>\n\n\n\n<p>Dictionaries cannot have two items with the same key:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Duplicate values will overwrite existing values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisdict =\u00a0{<br>\u00a0\u00a0\"brand\":\u00a0\"Ford\",<br>\u00a0\u00a0\"model\":\u00a0\"Mustang\",<br>\u00a0\u00a0\"year\":\u00a01964,<br>\u00a0\u00a0\"year\":\u00a02020<br>}<br>print(thisdict)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Dictionary Length<\/h2>\n\n\n\n<p>To determine how many items a dictionary has, use the&nbsp;<code>len()<\/code>&nbsp;function:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Print the number of items in the dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(len(thisdict))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Dictionary Items &#8211; Data Types<\/h2>\n\n\n\n<p>The values in dictionary items can be of any data type:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>String, int, boolean, and list data types:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisdict =\u00a0{<br>\u00a0\u00a0\"brand\":\u00a0\"Ford\",<br>\u00a0\u00a0\"electric\":\u00a0False,<br>\u00a0\u00a0\"year\":\u00a01964,<br>\u00a0\u00a0\"colors\": &#91;\"red\",\u00a0\"white\",\u00a0\"blue\"]<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">type()<\/h2>\n\n\n\n<p>From Python&#8217;s perspective, dictionaries are defined as objects with the data type &#8216;dict&#8217;:<\/p>\n\n\n\n<p>&lt;class &#8216;dict&#8217;&gt;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Print the data type of a dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisdict =\u00a0{<br>\u00a0\u00a0\"brand\":\u00a0\"Ford\",<br>\u00a0\u00a0\"model\":\u00a0\"Mustang\",<br>\u00a0\u00a0\"year\":\u00a01964<br>}<br>print(type(thisdict))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The dict() Constructor<\/h2>\n\n\n\n<p>It is also possible to use the&nbsp;dict()&nbsp;constructor to make a dictionary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Using the dict() method to make a dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisdict =\u00a0dict(name =\u00a0\"John\", age =\u00a036, country =\u00a0\"Norway\")<br>print(thisdict)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dictionary Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates. As of Python version 3.7, dictionaries are&nbsp;ordered. In Python 3.6 and earlier, dictionaries are&nbsp;unordered. Dictionaries are written with curly brackets, and have keys and values: ExampleGet your own Python Server [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107],"tags":[],"class_list":["post-1130","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=1130"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1130\/revisions"}],"predecessor-version":[{"id":1131,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1130\/revisions\/1131"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=1130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=1130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=1130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}