diff options
| author | 2021-05-03 16:14:30 -0700 | |
|---|---|---|
| committer | 2021-05-03 16:14:30 -0700 | |
| commit | 52be2e92cfe62a0ed35811ec73e22e3e63275e88 (patch) | |
| tree | 7cf14c0d63df79e3f03eb58bc6acfc6c546bbd84 | |
| parent | Move comments to their own line. (diff) | |
Removed opinions from text.
Co-authored-by: Boris Muratov <[email protected]>
| -rw-r--r-- | bot/resources/tags/str-join.md | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/bot/resources/tags/str-join.md b/bot/resources/tags/str-join.md index a6b8fb793..c835f9313 100644 --- a/bot/resources/tags/str-join.md +++ b/bot/resources/tags/str-join.md @@ -1,6 +1,6 @@ **Joining Iterables** -Suppose you want to nicely display a list (or some other iterable). The naive solution would be something like this. +If you want to display a list (or some other iterable), you can write: ```py colors = ['red', 'green', 'blue', 'yellow'] output = "" @@ -10,16 +10,16 @@ for color in colors: print(output) # Prints 'red, green, blue, yellow, ' ``` -However, this solution is flawed. The separator is still added to the last element, and it is slow. +However, the separator is still added to the last element, and it is relatively slow. -The better solution is to use `str.join`. +A better solution is to use `str.join`. ```py colors = ['red', 'green', 'blue', 'yellow'] separator = ", " print(separator.join(colors)) # Prints 'red, green, blue, yellow' ``` -This solution is much simpler, faster, and solves the problem of the extra separator. An important thing to note is that you can only `str.join` strings. For a list of ints, +An important thing to note is that you can only `str.join` strings. For a list of ints, you must convert each element to a string before joining. ```py integers = [1, 3, 6, 10, 15] |