mirror of
https://github.com/jbranchaud/til
synced 2026-07-03 08:08:24 +00:00
Add Create A Range Of Descending Values as a Python TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1759 TILs and counting..._
|
||||
_1760 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1047,6 +1047,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Check If Package Is Installed With Pip](python/check-if-package-is-installed-with-pip.md)
|
||||
- [Control Passing Of Time In Tests](python/control-passing-of-time-in-tests.md)
|
||||
- [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md)
|
||||
- [Create A Range Of Descending Values](python/create-a-range-of-descending-values.md)
|
||||
- [Dunder Methods](python/dunder-methods.md)
|
||||
- [Easy Key-Value Aggregates With defaultdict](python/easy-key-value-aggregates-with-defaultdict.md)
|
||||
- [Install With PIP For Specific Interpreter](python/install-with-pip-for-specific-interpreter.md)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# Create A Range Of Descending Values
|
||||
|
||||
A typical use of `range` looks something like this:
|
||||
|
||||
```python
|
||||
>>> list(range(1, 5))
|
||||
[1, 2, 3, 4]
|
||||
```
|
||||
|
||||
Which is equivalent to this one where we give a `step` value of `1`.
|
||||
|
||||
```python
|
||||
>>> list(range(1, 5, 1))
|
||||
[1, 2, 3, 4]
|
||||
```
|
||||
|
||||
If we try to create a _negative range_, that is, a range of values in decreasing
|
||||
order, we get an empty list.
|
||||
|
||||
```python
|
||||
>>> list(range(0, -7))
|
||||
[]
|
||||
```
|
||||
|
||||
That's because the `step` value still defaults to `1`. And there are no positive
|
||||
steps between `0` and `-7`. So, let's give `range` a `step` value of `-1`.
|
||||
|
||||
```python
|
||||
>>> list(range(0,-7, -1))
|
||||
[0, -1, -2, -3, -4, -5, -6]
|
||||
```
|
||||
|
||||
One practical use case of a negative range like this is using a list
|
||||
comprehension to transform it into a list of the _last seven days_.
|
||||
|
||||
```python
|
||||
>>> from datetime import datetime, timedelta
|
||||
>>> [datetime.now().date() + timedelta(days=days) for days in range(0,-7, -1)]
|
||||
[datetime.date(2026, 3, 19), datetime.date(2026, 3, 18), datetime.date(2026, 3, 17), datetime.date(2026, 3, 16), datetime.date(2026, 3, 15), datetime.date(2026, 3, 14), datetime.date(2026, 3, 13)]
|
||||
```
|
||||
|
||||
Of course this could have been written with a positive range and then
|
||||
subtracting the `timedelta`. I like that I have the option of doing this in
|
||||
whatever way makes the code most readable.
|
||||
Reference in New Issue
Block a user