aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar swfarnsworth <[email protected]>2020-07-08 15:56:49 -0400
committerGravatar swfarnsworth <[email protected]>2020-07-08 15:56:49 -0400
commitd6c775bc96d8b913677a87c9025a6194831d4b3b (patch)
tree1f9c2f742c2e2c50e1389fa006b94a276810d028
parentRe-lock Pipfile (diff)
Initial commit for proposed range-len command
Diffstat (limited to '')
-rw-r--r--bot/resources/tags/range-len.md19
1 files changed, 19 insertions, 0 deletions
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).