Python's "for" loops are single-purposed: they can loop over an iterable item-by-item. That's it.
The "for" loop itself can't loop in reverse or loop over multiple iterables.
For this reason, looping helpers are a VERY big deal in #Python.
Let's talk about looping helpers. 🧵
Want to loop over an iterable in the reverse direction? If it's a reversible iterable (lists, tuples, dictionaries, etc), you can use the reversed helper:
>>> colors = ["pink", "blue", "green"]
>>> for color in reversed(colors):
... print(color)
...
green
blue
pink
Note that the "for" loop isn't reversing here: reversed is!
The built-in reversed function accepts an iterable and returns a lazy iterable that provides each item from the original iterable in the reverse order.
>>> r = reversed(colors)
>>> next(r)
'green'
>>> next(r)
'blue'
Let's talk about what these methods do and why I recommend these ones first. 🧵
1️⃣ The string join method accepts an iterable of strings to join together.
It should be called on the separator you'd like to join by (e.g. " ", "\n", ",", ""). This is a string method: it's " ".join(my_list) and not my_list.join(" ").