|
1 | 1 | # About
|
2 | 2 |
|
3 |
| -A dictionary in Python is a data structure that associates [hashable][term-hashable] _keys_ to _values_ and can be known in other programming languages as a [hash table or hashmap][hashtable-wikipedia]. In Python, it's considered a [mapping type][mapping-types-dict]. `dicts` enable the retrieval of a value in constant time, given the key. |
4 | 3 |
|
5 |
| -Compared to searching for a value within a list or array (_without knowing the index position_), a dictionary uses significantly more memory, but has very rapid retrieval. It's especially useful in scenarios where the collection is large and must be accessed frequently. |
| 4 | +A dictionary (`dict`) is a [mapping type][mapping-types-dict] data structure that associates [hashable][term-hashable] `keys` to `values` -- known in other programming languages as a resizable [hash table or hashmap][hashtable-wikipedia]. |
| 5 | + `Keys` can include `numbers`, `str`, `tuples` (of _immutable_ values), or `frozensets`, but must be hashable and unique across the dictionary. |
| 6 | + `keys` are _immutable_ - once added to a `dict`, they can only be removed, they cannot be updated. |
| 7 | + `values` can be of any or multiple data type(s) or structures, including other dictionaries, built-in types, custom types, or even objects like functions or classes. |
| 8 | + `values` associated with any `key` are _mutable_, and can be replaced, updated or altered as long as the `key` entry exists. |
| 9 | + Dictionaries enable the retrieval of a `value` in (on average) constant O(1) time, given the `key`. |
6 | 10 |
|
7 |
| -## Keys and Values |
| 11 | + Compared to searching for a value within a `list` or `array` (_without knowing the `index` position_), a `dict` uses significantly more space in memory, but has significantly more rapid retrieval. |
| 12 | + Dictionaries are especially useful in scenarios where the collection of items is large and must be accessed and/or updated frequently. |
8 | 13 |
|
9 |
| -A dictionary can be though of as a collection of straightforward `key`:`value` pairs. Like other collection types (`list`, `tuple`, and `set`), `values` can contain arbitrary data and data types, including nested dictionaries. A dictionary’s `keys` are _**almost**_ as arbitrary, but require their data types to be _hashable_. |
| 14 | +## Dictionary creation |
10 | 15 |
|
11 |
| -Keys are unique across a dictionary, and the values associated with any `key` can be replaced, updated or altered as long as the `key` exists. To be a valid, a key (_and all its contained values_) must be [hashable][term-hashable]. Hashable `keys` can include numbers, strings, tuples of _immutable_ values, or frozensets. `sets`, `lists`, and `dicts` are unhashable and therefor cannot be used as keys. |
| 16 | +A simple `dict` can be declared using the literal form `{<key_1>: <value_1>, <key_2>: <value_2>}`: |
| 17 | + |
| 18 | + ```python |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +``` |
| 24 | + |
| 25 | + The dictionary constructor `dict(<key_1>=<value_1>, <key_2>=<value_2>)`, but there are many more ways of creating and initializing dictionaries including the use of a _dict comprehension_ or passing additional constructor parameters as illustrated in the [Python docs][mapping-types-dict]. |
12 | 26 |
|
13 |
| -A value can be of any data type, including built-in types, custom types or complex objects (even functions or classes). Commonly used objects are: numbers, strings, lists, dictionaries, tuples or sets. |
14 | 27 |
|
15 |
| -A simple `dict` can be declared using the literal form `{"key_1": value_1, "key_2": value_2}` or via _constructor_ with `dict(key_1=value_1, key_2=value_2)`, but there are many more ways of creating and initializing dictionaries including the use of a _dict comprehension_ or passing additional constructor parameters as illustrated in the [Python docs][mapping-types-dict]. |
16 | 28 |
|
17 | 29 | Inserting a new `key`:`value` pair can be done with `dict[key] = value` and the value can be retrieved by using `retrieved_value = dict[key]`.
|
18 | 30 |
|
|
0 commit comments