You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/dicts/about.md
+51-48Lines changed: 51 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,9 @@
3
3
A dictionary (`dict`) in Python is a data structure that associates [hashable][term-hashable]_keys_ to _values_ and is known in other programming languages as a resizable [hash table][hashtable-wikipedia], hashmap, or [associative array][associative-array].
4
4
Dictionaries are Python's only built-in [mapping type][mapping-types-dict].
5
5
6
-
`Keys` must be hashable and unique across the dictionary.
7
-
Key types can include `numbers`, `str`, or `tuples` (of _immutable_ values).
8
-
They cannot contain _mutable_ data structures such as `lists`, `dict`s, or `set`s.
6
+
`keys` must be hashable and unique across the dictionary.
7
+
Key types can include `number`s, `str`s, or `tuple`s (of _immutable_ values).
8
+
They cannot contain _mutable_ data structures such as `list`s, `dict`s, or `set`s.
9
9
As of Python 3.7, `dict` key order is guaranteed to be the order in which entries are inserted.
10
10
11
11
`values` can be of any data type or structure.
@@ -20,10 +20,10 @@ Dictionaries are especially useful in scenarios where the collection of items is
20
20
## Dictionary Construction
21
21
22
22
Dictionaries can be created in many different ways, including:
@@ -183,9 +186,9 @@ New `key`:`value` pairs can be _added_ in the same fashion:
183
186
184
187
## Removing (Pop-ing and del) Dictionary Entries
185
188
186
-
You can use the `.pop(<key>)` method to delete a dictionary entry.
187
-
`.pop()` removes the (`key`, `value`) pair and returns the `value` for use.
188
-
Like `.get()`, `.pop(<key>)` accepts second argument (_`dict.pop(<key>, <default value>)`_) that will be returned if the `key` is not found.
189
+
You can use the `<dict>.pop(<key>)` method to delete a dictionary entry.
190
+
`<dict>.pop()` removes the (`key`, `value`) pair and returns the `value` for use.
191
+
Like `<dict>.get()`, `<dict>.pop(<key>)` accepts second argument (_`<dict>.pop(<key>, <default value>)`_) that will be returned if the `key` is not found.
189
192
This prevents a `KeyError` being raised:
190
193
191
194
```python
@@ -208,8 +211,8 @@ KeyError: 'name'
208
211
'Unknown'
209
212
```
210
213
211
-
You can also use the `del` statement to remove a single or multiple entries.
212
-
A `KeError` is raised if the entry to be removed is not found in the dictionary:
214
+
You can also use the `del` statement to remove one or more entries.
215
+
A `KeyError` is raised if the entry to be removed is not found in the dictionary:
213
216
214
217
```python
215
218
>>> wombat = {'name': 'Wombat',
@@ -245,7 +248,7 @@ You can access _values_ within the same loop by using _square brackets_:
245
248
246
249
```python
247
250
>>>for key in bear:
248
-
>>>print((key, bear[key])) #this prints a tuple of (key, value)
251
+
...print((key, bear[key])) # <--This prints a tuple of (key, value).
249
252
('name', 'Black Bear')
250
253
('speed', 40)
251
254
('land_animal', True)
@@ -257,7 +260,7 @@ You can also use the `.items()` method, which returns (`key`, `value`) tuples:
257
260
# dict.items() forms (key, value tuples) that can be
258
261
# unpacked and iterated over.
259
262
>>>for key, value in whale.items():
260
-
>>>print(key, ":", value)
263
+
...print(key, ":", value)
261
264
name : Blue Whale
262
265
speed : 25
263
266
land_animal : False
@@ -271,11 +274,11 @@ For a detailed explanation of dictionaries in Python, the [official documentatio
271
274
272
275
## Extending Dictionary Functionality: The Collections Module
273
276
274
-
The [`collections`][collections-docs] module adds specialized functionality to Python's standard collection-based datatypes (`dictionary`, `set`, `list`, `tuple`).
277
+
The [`collections`][collections-docs] module adds specialized functionality to Python's standard collection-based datatypes (`dict`, `set`, `list`, `tuple`).
275
278
Three of the most useful dictionary-based classes are:
276
279
277
280
-[`Counter`][counter-dicts] automatically counts items and returns them in a `dict` with the items as keys and their counts as values.
278
-
-[`OrderedDict`][ordered-dicts-docs], has methods specialized for arranging the order of dictionary entries.
281
+
-[`OrderedDict`][ordered-dicts-docs] has methods specialized for arranging the order of dictionary entries.
279
282
-[`defaultdict`][default-dicts] uses a factory method to set a default value if a `key` is not found when trying to retrieve or assign to a dictionary entry.
0 commit comments