Skip to content

Commit ee37695

Browse files
committed
WIP typo and language edits for dicts concept.
1 parent 5536731 commit ee37695

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

Diff for: concepts/dicts/about.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
# About
22

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.
43

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`.
610

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.
813

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
1015

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].
1226

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.
1427

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].
1628

1729
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]`.
1830

Diff for: concepts/dicts/introduction.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
# Introduction
22

3-
A _**dictionary**_ is Python's primary mapping type that associates a _hashable key_ with a value. The lookup by key is more efficient than searching through an array, but does require more memory.
3+
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+
`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.
7+
Dictionaries enable the retrieval of a `value` in (on average) constant O(1) time, given the `key`.
8+
9+
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.
10+
Dictionaries are especially useful in scenarios where the collection of items is large and must be accessed and/or updated frequently.
11+
12+
13+
[hashtable-wikipedia]: https://en.wikipedia.org/wiki/Hash_table
14+
[mapping-types-dict]: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
15+
[term-hashable]: https://docs.python.org/3/glossary.html#term-hashable

0 commit comments

Comments
 (0)