From d6c775bc96d8b913677a87c9025a6194831d4b3b Mon Sep 17 00:00:00 2001 From: swfarnsworth Date: Wed, 8 Jul 2020 15:56:49 -0400 Subject: Initial commit for proposed range-len command --- bot/resources/tags/range-len.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 bot/resources/tags/range-len.md diff --git a/bot/resources/tags/range-len.md b/bot/resources/tags/range-len.md new file mode 100644 index 000000000..b1c973647 --- /dev/null +++ b/bot/resources/tags/range-len.md @@ -0,0 +1,19 @@ +Iterating over `range(len(...))` is a common approach to accessing each item +in an ordered collection. + +```py +for i in range(len(my_list)): + do_something(my_list[i]) +``` + +The pythonic syntax is much simpler, and is +guaranteed to produce elements in the same order: + +```py +for item in my_list: + do_something(item) +``` + +Python has other solutions for cases when the index itself might be needed. +To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). +To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). -- cgit v1.2.3 From 9174125a41793d4703a81dc6783f4244f1634d27 Mon Sep 17 00:00:00 2001 From: swfarnsworth Date: Wed, 8 Jul 2020 17:51:04 -0400 Subject: Removed hard line breaks --- bot/resources/tags/range-len.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/bot/resources/tags/range-len.md b/bot/resources/tags/range-len.md index b1c973647..9b88aab47 100644 --- a/bot/resources/tags/range-len.md +++ b/bot/resources/tags/range-len.md @@ -1,19 +1,15 @@ -Iterating over `range(len(...))` is a common approach to accessing each item -in an ordered collection. +Iterating over `range(len(...))` is a common approach to accessing each item in an ordered collection. ```py for i in range(len(my_list)): do_something(my_list[i]) ``` -The pythonic syntax is much simpler, and is -guaranteed to produce elements in the same order: +The pythonic syntax is much simpler, and is guaranteed to produce elements in the same order: ```py for item in my_list: do_something(item) ``` -Python has other solutions for cases when the index itself might be needed. -To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). -To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). +Python has other solutions for cases when the index itself might be needed. To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). -- cgit v1.2.3 From 8cba21c353a728d2c09ad82a425c46ce3f03abf0 Mon Sep 17 00:00:00 2001 From: Steele Farnsworth <32915757+swfarnsworth@users.noreply.github.com> Date: Thu, 9 Jul 2020 11:20:01 -0400 Subject: Update range-len.md Removed all blank lines to improve how it's rendered on Discord; thanks @kwzrd for rendering this! --- bot/resources/tags/range-len.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bot/resources/tags/range-len.md b/bot/resources/tags/range-len.md index 9b88aab47..65665eccf 100644 --- a/bot/resources/tags/range-len.md +++ b/bot/resources/tags/range-len.md @@ -1,15 +1,11 @@ Iterating over `range(len(...))` is a common approach to accessing each item in an ordered collection. - ```py for i in range(len(my_list)): do_something(my_list[i]) ``` - The pythonic syntax is much simpler, and is guaranteed to produce elements in the same order: - ```py for item in my_list: do_something(item) ``` - Python has other solutions for cases when the index itself might be needed. To get the element at the same index from two or more lists, use [zip](https://docs.python.org/3/library/functions.html#zip). To get both the index and the element at that index, use [enumerate](https://docs.python.org/3/library/functions.html#enumerate). -- cgit v1.2.3