Skip to content

Commit ed70b14

Browse files
authored
[Sets and Dict Methods Concepts]: Typo & Formatting Fixes (exercism#4185)
* Typo and formatting fixes for stets and dictmethods. * Applied changes from code review.
1 parent 4a3ca80 commit ed70b14

4 files changed

Lines changed: 42 additions & 42 deletions

File tree

concepts/dict-methods/about.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# Dictionary Methods in Python
22

33
The `dict` class in Python provides many useful [methods][dict-methods] for working with dictionaries.
4-
Some were introduced in the concept for `dicts`.
4+
Some were introduced in the concept for `dict`s.
55
Here we cover a few more - along with some techniques for iterating through and manipulating dictionaries.
66

7-
- `dict.setdefault()` automatically adds keys without throwing a KeyError.
8-
- `dict.fromkeys(iterable, <default value>)` creates a new `dict` from any number of iterables.
9-
- `.keys()`, `.values()`, and `.items()` provide convenient iterators.
10-
- `sorted(<dict>.items())`. can easily re-order entries in a `dict`.
11-
- `dict_one.update(<dict_two>)` updates one `dict` with overlapping values from another `dict`.
12-
- `dict | other_dict` and `dict |= other_dict` merges or updates two `dict`s via operators.
13-
- `reversed(dict.keys())`, `reversed(dict.values())`, or `reversed(dict.items())` produce _reversed_ views.
7+
- `<dict>.setdefault()` automatically adds keys without throwing a KeyError.
8+
- `<dict>.fromkeys(<iterable>, <default value>)` creates a new `dict` from any number of iterables.
9+
- `<dict>.keys()`, `<dict>.values()`, and `<dict>.items()` provide convenient iterators.
10+
- `sorted(<dict>.items())` can easily re-order entries in a `dict`.
11+
- `<dict_one>.update(<dict_two>)` updates one `dict` with overlapping values from another `dict`.
12+
- `<dict> | <other_dict>` and `<dict> |= <other_dict>` merges or updates two `dict`s via operators.
13+
- `reversed(<dict>.keys())`, `reversed(<dict>.values())`, or `reversed(<dict>.items())` produce _reversed_ views.
1414
- `<dict>.popitem()` removes and returns a `key`, `value` pair.
1515

1616

1717
## `setdefault()` for Error-Free Insertion
1818

19-
The dictionary concept previously covered that `.get(key, <default value>)` returns an existing `value` or the `default value` if a `key` is not found in a dictionary, thereby avoiding a `KeyError`.
19+
The dictionary concept previously covered that `<dict>.get(<key>, <default value>)` returns an existing `value` or the `default value` if a `key` is not found in a dictionary, thereby avoiding a `KeyError`.
2020
This works well in situations where you would rather not have extra error handling but cannot trust that a looked-for `key` will be present.
2121

22-
For a similarly "safe" (_without KeyError_) insertion operation, there is the `.setdefault(key, <default value>)` method.
23-
`setdefault(key, <default value>)` will return the `value` if the `key` is found in the dictionary.
22+
For a similarly "safe" (_without `KeyError`_) insertion operation, there is the `<dict>.setdefault(<key>, <default value>)` method.
23+
`<dict>.setdefault(<key>, <default value>)` will return the `value` if the `key` is found in the dictionary.
2424
If the key is **not** found, it will _insert_ the (`key`, `default value`) pair and return the `default value` for use.
2525

26+
2627
```python
2728
>>> palette_I = {'Grassy Green': '#9bc400', 'Purple Mountains Majesty': '#8076a3', 'Misty Mountain Pink': '#f9c5bd'}
2829

@@ -38,7 +39,7 @@ If the key is **not** found, it will _insert_ the (`key`, `default value`) pair
3839

3940
## `fromkeys()` to Populate a Dictionary from an Iterable
4041

41-
To quickly populate a dictionary with various `keys` and default values, the _class method_ [`fromkeys(iterable, <default value>)`][fromkeys] will iterate through an iterable of `keys` and create a new `dict`.
42+
To quickly populate a dictionary with various `keys` and default values, the _class method_ [`fromkeys(<iterable>, <default value>)`][fromkeys] will iterate through an iterable of `keys` and create a new `dict`.
4243
All `values` will be set to the `default value` provided:
4344

4445
```python
@@ -71,13 +72,12 @@ If the dictionary is empty, calling `popitem()` will raise a `KeyError`:
7172
# All (key, value) pairs have been removed.
7273
>>> palette_I.popitem()
7374
Traceback (most recent call last):
74-
7575
line 1, in <module>
7676
palette_I.popitem()
77-
7877
KeyError: 'popitem(): dictionary is empty'
7978
```
8079

80+
8181
## Iterating Over Entries in a Dictionary Via Views
8282

8383
The `.keys()`, `.values()`, and `.items()` methods return [_iterable views_][dict-views] of a dictionary.
@@ -136,7 +136,7 @@ This allows keys, values, or (`key`, `value`) pairs to be iterated over in Last-
136136
('Purple baseline', '#161748')
137137

138138
>>> for item in reversed(palette_II.items()):
139-
... print (item)
139+
... print(item)
140140
...
141141
('Purple baseline', '#161748')
142142
('Green Treeline', '#478559')
@@ -166,12 +166,12 @@ This method will take the (`key`,`value`) pairs of `<dict_two>` and write them i
166166
Where keys in the two dictionaries _overlap_, the `value` in `dict_one` will be _overwritten_ by the corresponding `value` from `dict_two`:
167167

168168
```python
169-
>>> palette_I = {'Grassy Green': '#9bc400',
170-
'Purple Mountains Majesty': '#8076a3',
171-
'Misty Mountain Pink': '#f9c5bd',
172-
'Factory Stone Purple': '#7c677f',
173-
'Green Treeline': '#478559',
174-
'Purple baseline': '#161748'}
169+
>>> palette_I = {'Grassy Green': '#9bc400',
170+
'Purple Mountains Majesty': '#8076a3',
171+
'Misty Mountain Pink': '#f9c5bd',
172+
'Factory Stone Purple': '#7c677f',
173+
'Green Treeline': '#478559',
174+
'Purple baseline': '#161748'}
175175

176176
>>> palette_III = {'Grassy Green': (155, 196, 0),
177177
'Purple Mountains Majesty': (128, 118, 163),
@@ -333,10 +333,10 @@ If the values stored in the `dict` are not unique, extra checks become necessary
333333

334334
# Iterating over (key, value) pairs using .items()
335335
>>> for key, value in extended_color_reference.items():
336-
... if value in consolidated_colors: #Check if key has already been created.
336+
... if value in consolidated_colors: # <--Check if key has already been created.
337337
... consolidated_colors[value].append(key)
338338
... else:
339-
... consolidated_colors[value] = [key] #Create a value list with the former key in it.
339+
... consolidated_colors[value] = [key] # <--Create a value list with the former key in it.
340340

341341
>>> consolidated_colors
342342
{'Purple Mountains Majesty': ['#8076a3', (128, 118, 163), (21, 28, 0, 36)],

concepts/dict-methods/introduction.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ The `dict` class in Python provides many useful [methods][dict-methods], some of
44

55
This concept tackles a few more:
66

7-
- `dict.setdefault()` automatically adds keys without throwing a `KeyError`.
8-
- `dict.fromkeys(iterable, <default value>)` creates a new `dict` from any number of iterables.
9-
- `.keys()`, `.values()`, and `.items()` provide convenient iterators.
10-
- `sorted(<dict>.items())`. can easily re-order entries in a `dict`.
11-
- `dict_one.update(<dict_two>)` updates one `dict` with overlapping values from another `dict`.
12-
- `dict | other_dict` and `dict |= other_dict` merges or updates two `dict`s via operators.
13-
- `reversed(dict.keys())`, `reversed(dict.values())`, or `reversed(dict.items())` produce _reversed_ views.
7+
- `<dict>.setdefault()` automatically adds keys without throwing a KeyError.
8+
- `<dict>.fromkeys(<iterable>, <default value>)` creates a new `dict` from any number of iterables.
9+
- `<dict>.keys()`, `<dict>.values()`, and `<dict>.items()` provide convenient iterators.
10+
- `sorted(<dict>.items())` can easily re-order entries in a `dict`.
11+
- `<dict_one>.update(<dict_two>)` updates one `dict` with overlapping values from another `dict`.
12+
- `<dict> | <other_dict>` and `<dict> |= <other_dict>` merges or updates two `dict`s via operators.
13+
- `reversed(<dict>.keys())`, `reversed(<dict>.values())`, or `reversed(<dict>.items())` produce _reversed_ views.
1414
- `<dict>.popitem()` removes and returns a `key`, `value` pair.
1515

1616
[dict-methods]: https://docs.python.org/3/library/stdtypes.html#dict

concepts/sets/about.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
A [`set`][type-set] is a _mutable_ and _unordered_ collection of [_hashable_][hashable] objects.
44
Set members must be distinct — duplicate items are not allowed.
55
They can hold multiple different data types and even nested structures like a `tuple` of `tuples` — as long as all elements can be _hashed_.
6-
Sets also come in an immutable [`frozensets`][type-frozenset] flavor.
6+
Sets also come in an immutable [`frozenset`][type-frozenset] flavor.
77

88
Sets are most commonly used to quickly remove duplicates from other data structures or item groupings.
99
They are also used for efficient comparisons when sequencing and duplicate tracking are not needed.
1010

1111
Like other collection types (_dictionaries, lists, tuples_), `sets` support:
12-
- Iteration via `for item in <set>`
12+
- Iteration via `for item in <set>`,
1313
- Membership checking via `in` and `not in`,
1414
- Length calculation through `len()`, and
15-
- Shallow copies through `copy()`
15+
- Shallow copies through `copy()`.
1616

1717
`sets` do not support:
1818
- Indexing of any kind
@@ -174,12 +174,12 @@ Traceback (most recent call last):
174174

175175
Sets have methods that generally mimic [mathematical set operations][mathematical-sets].
176176
Most (_not all_) of these methods have an [operator][operator] equivalent.
177-
Methods generally take any `iterable` as an argument, while operators require that both sides of the operation are `sets` or `frozensets`.
177+
Methods generally take any `iterable` as an argument, while operators require that both sides of the operation are `set`s or `frozenset`s.
178178

179179

180180
### Membership Testing Between Sets
181181

182-
The `<set>.isdisjoint(<other_collection>)` method is used to test if a `sets` elements have any overlap with the elements of another.
182+
The `<set>.isdisjoint(<other_collection>)` method is used to test if a `set`'s elements have any overlap with the elements of another.
183183
The method will accept any `iterable` or `set` as an argument.
184184
It will return `True` if the two sets have **no elements in common**, `False` if elements are **shared**.
185185

@@ -275,9 +275,9 @@ True
275275
### 'Proper' Subsets and Supersets
276276

277277
`<set> < <other_set>` and `<set> > <other_set>` are used to test for _proper subsets_.
278-
A `set` is a proper subset if (`<set>` <= `<other_set>`) **AND** (`<set>` != `<other_set>`) for the `<` operator.
278+
A `set` is a proper subset if (`<set> <= <other_set>`) **AND** (`<set> != <other_set>`) for the `<` operator.
279279

280-
A `set is a proper superset if `(`<set>` >= `<other_set>`) **AND** (`<set>` != `<other_set>`) for the `>` operator.
280+
A `set` is a proper superset if (`<set> >= <other_set>`) **AND** (`<set> != <other_set>`) for the `>` operator.
281281
These operators have no method equivalent:
282282

283283
```python
@@ -336,7 +336,7 @@ The operator form of this method is `<set> | <other set 1> | <other set 2> | ...
336336
### Set Differences
337337

338338
`<set>.difference(*<other iterables>)` returns a new `set` with elements from the original `<set>` that are not in `<others>`.
339-
The operator version of this method is `<set> - <other set 1> - <other set 2> - ...<other set n>`.
339+
The operator version of this method is `<set> - <other set 1> - <other set 2> - ... - <other set n>`.
340340

341341
```python
342342
>>> berries_and_veggies = {'Asparagus',
@@ -370,7 +370,7 @@ The operator version of this method is `<set> - <other set 1> - <other set 2> -
370370
### Set Intersections
371371

372372
`<set>.intersection(*<other iterables>)` returns a new `set` with elements common to the original `set` and all `<others>` (in other words, the `set` where everything [intersects][intersection]).
373-
The operator version of this method is `<set> & <other set> & <other set 2> & ... <other set n>`
373+
The operator version of this method is `<set> & <other set> & <other set 2> & ... & <other set n>`
374374

375375
```python
376376
>>> perennials = {'Annatto','Asafetida','Asparagus','Azalea',

concepts/sets/introduction.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
A [`set`][type-set] is a _mutable_ and _unordered_ collection of [_hashable_][hashable] objects.
44
Set members must be distinct — duplicate items are not allowed.
55
They can hold multiple different data types and even nested structures like a `tuple` of `tuples` — as long as all elements can be _hashed_.
6-
Sets also come in an immutable [`frozensets`][type-frozenset] flavor.
6+
Sets also come in an immutable [`frozenset`][type-frozenset] flavor.
77

88
Sets are most commonly used to quickly remove duplicates from other data structures or item groupings.
99
They are also used for efficient comparisons when sequencing and duplicate tracking are not needed.
1010

1111
Like other collection types (_dictionaries, lists, tuples_), `sets` support:
12-
- Iteration via `for item in <set>`
12+
- Iteration via `for item in <set>`,
1313
- Membership checking via `in` and `not in`,
1414
- Length calculation through `len()`, and
15-
- Shallow copies through `copy()`
15+
- Shallow copies through `copy()`.
1616

1717
`sets` do not support:
1818
- Indexing of any kind

0 commit comments

Comments
 (0)