Skip to content

Commit fa51110

Browse files
authored
[Dicts Concept]: Fixed Typos & Grammar Errors in Docs (exercism#4177)
* Fixed typos and grammar errors in dicts concept. * Applied corrections from code review.
1 parent ace990c commit fa51110

2 files changed

Lines changed: 54 additions & 51 deletions

File tree

concepts/dicts/about.md

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
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].
44
Dictionaries are Python's only built-in [mapping type][mapping-types-dict].
55

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.
99
As of Python 3.7, `dict` key order is guaranteed to be the order in which entries are inserted.
1010

1111
`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
2020
## Dictionary Construction
2121

2222
Dictionaries can be created in many different ways, including:
23-
- Using the [`fromkeys()`][fromkeys] classmethod
24-
- Creating [dictionary comprehensions][dict-comprehensions]
25-
- Merging two dictionaries via unpacking (`**`)
26-
- Merging dictionaries via the `|` (_update_) operator
23+
- Using the [`fromkeys()`][fromkeys] class method.
24+
- Using [dictionary comprehensions][dict-comprehensions].
25+
- Merging two dictionaries via unpacking (`**`).
26+
- Merging dictionaries via the `|` (_update_) operator.
2727
- Using a loop to iteratively add entries to a previously created empty `dict`.
2828

2929
The two most straightforward methods are the dictionary _constructor_ and the dictionary _literal_.
@@ -35,17 +35,20 @@ The two most straightforward methods are the dictionary _constructor_ and the di
3535

3636
```python
3737
# Passing a list of key,value tuples.
38-
>>> wombat = dict([('name', 'Wombat'),('speed', 23),
39-
('land_animal', True)])
38+
>>> wombat = dict([('name', 'Wombat'),
39+
... ('speed', 23),
40+
... ('land_animal', True)])
4041
{'name': 'Wombat', 'speed': 23, 'land_animal': True}
4142

4243

4344
# Using key=value arguments.
44-
>>> bear = dict(name="Black Bear", speed=40, land_animal=True)
45+
>>> bear = dict(name="Black Bear",
46+
... speed=40,
47+
... land_animal=True)
4548
{'name': 'Black Bear', 'speed': 40, 'land_animal': True}
4649
```
4750

48-
The [documentation on `dicts`][dicts-docs] outlines additional variations and options in constructor use.
51+
The [documentation on `dict`s][dicts-docs] outlines additional variations and options in constructor use.
4952

5053

5154
### Dictionary Literals
@@ -74,35 +77,35 @@ Dictionaries can be arbitrarily nested:
7477

7578
```python
7679
animals = {
77-
"Real" : {
78-
"Winged" : {
79-
"Sparrow" : {'name': 'sparrow','speed': 12, 'land_animal': True},
80-
"Kestrel" : {'name': 'kestrel', 'speed': 15, 'land_animal': True}
81-
},
82-
"Legged" : {
83-
"Wombat" : {'name': 'Wombat', 'speed': 23, 'land_animal': True},
84-
"Black Bear": {'name': 'Black Bear', 'speed': 40, 'land_animal': True},
85-
"Polecat" : {'name': 'Polecat', 'speed': 15, 'land_animal': True}
86-
},
87-
"Other" : {
88-
"Whale" : {'name': 'Blue Whale', 'speed': 35, 'land_animal': False},
89-
"Orca" : {'name': 'Orca', 'speed': 45, 'land_animal': False},
90-
"Snake" : {'name': 'Python', 'speed': 25, 'land_animal': True}
91-
}
92-
},
80+
"Real" : {
81+
"Winged" : {
82+
"Sparrow" : {'name': 'sparrow','speed': 12, 'land_animal': True},
83+
"Kestrel" : {'name': 'kestrel', 'speed': 15, 'land_animal': True}
84+
},
85+
"Legged" : {
86+
"Wombat" : {'name': 'Wombat', 'speed': 23, 'land_animal': True},
87+
"Black Bear": {'name': 'Black Bear', 'speed': 40, 'land_animal': True},
88+
"Polecat" : {'name': 'Polecat', 'speed': 15, 'land_animal': True}
89+
},
90+
"Other" : {
91+
"Whale" : {'name': 'Blue Whale', 'speed': 35, 'land_animal': False},
92+
"Orca" : {'name': 'Orca', 'speed': 45, 'land_animal': False},
93+
"Snake" : {'name': 'Python', 'speed': 25, 'land_animal': True}
94+
}
95+
},
9396

94-
"Imaginary": {
95-
"Winged" : {
96-
"Dragon" : {'name': 'Fire Dragon','speed': 100, 'land_animal': True},
97-
"Phoenix" : {'name': 'Phoenix', 'speed': 1500, 'land_animal': True}
98-
},
99-
"Legged" : {
100-
"Sphinx" : {'name': 'Sphinx','speed': 10, 'land_animal': True},
101-
"Minotaur" : {'name': 'Minotaur', 'speed': 5, 'land_animal': True}
102-
},
103-
"Other" : {}
104-
}
105-
}
97+
"Imaginary": {
98+
"Winged" : {
99+
"Dragon" : {'name': 'Fire Dragon','speed': 100, 'land_animal': True},
100+
"Phoenix" : {'name': 'Phoenix', 'speed': 1500, 'land_animal': True}
101+
},
102+
"Legged" : {
103+
"Sphinx" : {'name': 'Sphinx','speed': 10, 'land_animal': True},
104+
"Minotaur" : {'name': 'Minotaur', 'speed': 5, 'land_animal': True}
105+
},
106+
"Other" : {}
107+
}
108+
}
106109
```
107110

108111
## Accessing Values in a `dict`
@@ -183,9 +186,9 @@ New `key`:`value` pairs can be _added_ in the same fashion:
183186

184187
## Removing (Pop-ing and del) Dictionary Entries
185188

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.
189192
This prevents a `KeyError` being raised:
190193

191194
```python
@@ -208,8 +211,8 @@ KeyError: 'name'
208211
'Unknown'
209212
```
210213

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:
213216

214217
```python
215218
>>> wombat = {'name': 'Wombat',
@@ -245,7 +248,7 @@ You can access _values_ within the same loop by using _square brackets_:
245248

246249
```python
247250
>>> 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).
249252
('name', 'Black Bear')
250253
('speed', 40)
251254
('land_animal', True)
@@ -257,7 +260,7 @@ You can also use the `.items()` method, which returns (`key`, `value`) tuples:
257260
# dict.items() forms (key, value tuples) that can be
258261
# unpacked and iterated over.
259262
>>> for key, value in whale.items():
260-
>>> print(key, ":", value)
263+
... print(key, ":", value)
261264
name : Blue Whale
262265
speed : 25
263266
land_animal : False
@@ -271,11 +274,11 @@ For a detailed explanation of dictionaries in Python, the [official documentatio
271274

272275
## Extending Dictionary Functionality: The Collections Module
273276

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`).
275278
Three of the most useful dictionary-based classes are:
276279

277280
- [`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.
279282
- [`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.
280283

281284
[associative-array]: https://en.wikipedia.org/wiki/Associative_array#:~:text=In%20computer%20science%2C%20an%20associative,a%20function%20with%20finite%20domain.

concepts/dicts/introduction.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ A dictionary (`dict`) in Python is a data structure that associates [hashable][t
44
Dictionaries are Python's only built-in [mapping type][mapping-types-dict].
55

66

7-
`Keys` must be hashable and unique across the dictionary.
8-
Key types can include `numbers`, `str`, or `tuples` (of _immutable_ values).
9-
They cannot contain _mutable_ data structures such as `lists`, `dict`s, or `set`s.
7+
`keys` must be hashable and unique across the dictionary.
8+
Key types can include `number`s, `str`s, or `tuple`s (of _immutable_ values).
9+
They cannot contain _mutable_ data structures such as `list`s, `dict`s, or `set`s.
1010
As of Python 3.7, `dict` key order is guaranteed to be the order in which entries are inserted.
1111

1212
`values` can be of any data type or structure.

0 commit comments

Comments
 (0)