Is list[i:j] guaranteed to be an empty list if list[j] precedes list[i]?

Yes, if j <= i is true, the resulting slice is empty, for standard Python types. To get the results in reverse order, you need to add a negative stride:

list[i:j:-1]

because explicit is better than implicit.

This is documented in Common Sequence Operations, footnote 4:

The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty.

Bold emphasis mine.

Custom types are free to interpret this differently.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)