visit
Second to a Python list, the dictionary or “dict” is a place in memory to store a series of values – also called a collection. The dictionary is special because values are not referenced in order using a numerical index. Rather, in a dictionary, values are referenced with a user-defined key, just as words in a physical dictionary are “keys” associated with the “value” of their meaning. This key is usually a string, but could be any number of data types.
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">my_dict = {<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'my_key'</span> : <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'my_value'</span>}</code>
For example, instead of referring to the first value in a list with my_list[0], one refers to any dictionary element by its key:
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);"><span class="hljs-meta" style="box-sizing: border-box; color: rgb(153, 153, 153); font-weight: 700;">>>> </span>my_dict[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'my_key'</span>]
‘my_value’</code>
Additionally, key-value combinations allow complex hierarchies of nested data. As words in a dictionary are keys to the values of their definitions, so letters of the alphabet are keys to the values of words themselves. Such complexity in data in structure is often necessary, when dealing with complex data. With this special feature, a dictionary lives somewhere between lists and user-defined classes. Python dictionaries are more feature-rich than lists, but don’t require as much effort as a user-defined class with unique attributes and methods.
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">my_dict = {<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'key1'</span>: <span class="hljs-number" style="box-sizing: border-box; color: teal;">1</span>, <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'key2'</span>: <span class="hljs-number" style="box-sizing: border-box; color: teal;">2</span>}</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">my_dict = dict(key1 = <span class="hljs-number" style="box-sizing: border-box; color: teal;">1</span>, key2 = <span class="hljs-number" style="box-sizing: border-box; color: teal;">2</span>)</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">my_dict = {}
my_dict = dict()</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">my_dict[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'key'</span>] = <span class="hljs-number" style="box-sizing: border-box; color: teal;">123</span>
<span class="hljs-meta" style="box-sizing: border-box; color: rgb(153, 153, 153); font-weight: 700;">>>> </span>my_dict
{<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'key'</span>: <span class="hljs-number" style="box-sizing: border-box; color: teal;">123</span>}</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">my_dict = {
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'my_nested_dict'</span>:
{
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'a_key'</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'a_value'</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'another_key'</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'another_value'</span>,
}
}</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">my_variable = my_dict[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'my_nested_dict'</span>]</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">automatic_dictionary = {key: value <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">for</span> (key, value) <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">in</span> < some_iterable >}</code>
Want to Code Faster?Kite is a plugin for PyCharm, Atom, Vim, VSCode, Sublime Text, and IntelliJ that uses machine learning to provide you with code completions in real time sorted by relevance. Start coding faster today.Download Kite Free
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: 700;">User</span><span class="hljs-params" style="box-sizing: border-box;">(object)</span>:</span>
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">""" Stores info about Users """</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(153, 0, 0); font-weight: 700;">__init__</span><span class="hljs-params" style="box-sizing: border-box;">(self, name, email, address, password, url)</span>:</span>
self.name = name
self.email = email
...
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(153, 0, 0); font-weight: 700;">send_email</span><span class="hljs-params" style="box-sizing: border-box;">(self)</span>:</span>
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">""" Send an email to our user"""</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">pass</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(153, 0, 0); font-weight: 700;">__repr__</span><span class="hljs-params" style="box-sizing: border-box;">()</span>:</span>
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"""Logic to properly format data"""</span>
bill = User(<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'Bill'</span>, <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'bill @ gmail.com'</span>, <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'123 Acme Dr.'</span>, <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'secret-password'</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'http: // www.bill.com'</span>)
bill.send_email()</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">bill = {<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'email'</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'[email protected]'</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'address'</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'123 Acme Dr.'</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'password'</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'secret-password'</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'url'</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'//www.bill.com'</span>}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(153, 0, 0); font-weight: 700;">send_email</span><span class="hljs-params" style="box-sizing: border-box;">(user_dict)</span>:</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">pass</span>
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;"># smtp email logic …</span>
send_email(bill[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'email'</span>]) <span class="hljs-comment" style="box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;"># bracket notation or …</span>
send_email(bill.get(<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'email'</span>)) <span class="hljs-comment" style="box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;"># .get() method is handy, too</span></code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">json_response = [{
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"id"</span>: <span class="hljs-number" style="box-sizing: border-box; color: teal;">1</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"first_name"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"Florentia"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"last_name"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"Schelle"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"email"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"[email protected]"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"url"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"//wired.com"</span>
}, {
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"id"</span>: <span class="hljs-number" style="box-sizing: border-box; color: teal;">2</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"first_name"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"Montague"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"last_name"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"McAteer"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"email"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"[email protected]"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"url"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"//domainmarket.com"</span>
}, {
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"id"</span>: <span class="hljs-number" style="box-sizing: border-box; color: teal;">3</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"first_name"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"Dav"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"last_name"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"Yurin"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"email"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"[email protected]"</span>,
<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"url"</span>: <span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">"//wufoo.com"</span>
}]</code>
<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">users = []
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">for</span> i <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(51, 51, 51); font-weight: 700;">in</span> json_response:
users.append(User(
name=i[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'first_name'</span>] + i[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'last_name'</span>],
email = i[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'email'</span>],
url=i[<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'url'</span>],
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;"># ...</span>
))</code>
The advantage of less rigid structures is that specific values are easily accessible. The disadvantage is that sets of values on a corresponding “level” of nesting under other keys are more difficult to relate to each other, and the resulting code is more verbose. If data naturally falls into columns and rows, then something like a Pandas DataFrame or a Numpy ndarray would be more appropriate, allowing values to be referenced by their relative location in vector space.
JSON is for SerializationWhile Python developers are used to manipulating Python objects in memory, JSON is a different story. Instead, JSON is a standard for serializing all sorts of data to send like a telegram over HTTP. Once JSON makes it across the wire, it can be deserialized, or loaded into a Python object.JSON can be a StringBefore JSON objects make it into Python logic, they are strings usually sent as a response to an HTTP request, and then parsed in various ways. JSON responses usually look like lists of dictionaries surrounded by quotes. Conveniently, lists of dictionaries can be easily parsed into even more useful objects like Pandas DataFrames (Pandas is a powerful data analysis tool for Python). Whenever loading and dumping (serializing) JSON objects, at some point they will become strings in Python.Duplicate KeysPython dictionary keys must be unique. In other words, some_dictionary.keys() will be a set of unique values. This is not the case for JSON – which is a bit unusual as it seems to defeat the purpose of keys in the first place – but no one ever said JSON was pythoic. Duplicate keys must be explicitly handled when converting JSON to a Python object, or only one key-value pair will make it through.Want to Code Faster?Kite is a plugin for PyCharm, Atom, Vim, VSCode, Sublime Text, and IntelliJ that uses machine learning to provide you with code completions in real time sorted by relevance. Start coding faster today.Download Kite Free
<code class="html hljs xml" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">>>> print(my_dict['my_key'])
Traceback (most recent call last):
File "<span class="hljs-tag" style="box-sizing: border-box; color: navy; font-weight: 400;"><<span class="hljs-name" style="box-sizing: border-box; color: navy; font-weight: 400;">input</span>></span>", line 1, in <span class="hljs-tag" style="box-sizing: border-box; color: navy; font-weight: 400;"><<span class="hljs-name" style="box-sizing: border-box; color: navy; font-weight: 400;">module</span>></span>
KeyError: 'my_key'</code>
Since a program may often just need to “check” for the existence of a key-value pair without throwing an error, a developer has other options. The first is to import the defaultdict object from the collections module, a handy override automatically populated with default values. Rather than showing an error, the default value is returned.
Secondly, the .get() method on a standard dictionary may return any value passed as the second argument. So, instead of bracket notation, referencing a value looks like …<code class="Python hljs livecodeserver" style="box-sizing: border-box; font-size: 1.75rem; font-family: "Input Sans Narrow"; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border-radius: 0px; line-height: 25px; padding: 0.5em 16rem; white-space: pre; font-feature-settings: "zero"; letter-spacing: 0px; display: block; overflow-x: auto; border-top: 1px solid rgb(224, 224, 225); border-bottom: 1px solid rgb(224, 224, 225);">just_checking = my_dict.get(<span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">'my_key’, None)</span><span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">
>>> print(just_checking)</span><span class="hljs-string" style="box-sizing: border-box; color: rgb(221, 17, 68);">
None</span></code>
Dictionaries are defined as “unordered” collections of key-value pairs, which can be inconvenient. To add ordered behavior, we have the OrderedDict, also from the collections module. As the name implies, an OrderedDict maintains returns pairs in the order they are defined.
While not as lightweight as the standard dictionary, many developers prefer to use OrderedDict, as it behaves in a more predictable way. When iterating through a standard dictionary, the key-value pairs will be returned in a random order. An OrderedDict always returns pairs in the same order, which can be helpful when looking for specific pairs in a large dataset. Proponents of defaultdict and OrderedDict don’t ask “Why?” – they ask “Why not?”This article was originally published at .