You could try

a = list(zip(a[:-1], a[1:]))
a.append((a[-1], a[0])])

That gives you a list of tuples. If you want a list of lists, just do

>>> a = [list(i) for i in zip(a[:-1], a[1:])]
>>> a.append([a[-1], a[0]])

(Edited version taking 1. the [a[-1],a[0]] out of the list comprehension and 2. the fact that zip doesn't return a list in Python 3+)

Yet another possibility would be to just append the first value to the list:

>>> tmp = a + a[0]
>>> list(zip(tmp[:,-1], tmp[1:]))

It can be argued that it creates a temporary list. You could do aa.append(a[0]) instead but then you'd modify your initial list, which might be an issue.


For creating a range like this, I might try a generator:

def make_ranges(lst):
    max_idx = len(lst) - 1
    for i,item in enumerate(lst):
        yield [item, lst[i+1 if i != max_idx else 0] ]

You could play around with keyword arguments to tell make_ranges whether or not to be periodic and if periodic, whether or not to include the wraparound at the beginning or the end.

Then you can make your ranges:

a = [1,2,3,4,5,6,7,8]
b = list(make_ranges(a))
print(b == [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,1]]) #True

One more option1 as a one-liner: but with a really unpythonic expression (imho) so that you don't have to append the last and first pair in a separate step:

>>> [list(i) for i in zip(a, a[1:]+[a[0]])]
[[datetime.time(0, 0), datetime.time(8, 0)],
 [datetime.time(8, 0), datetime.time(13, 0)],
 [datetime.time(13, 0), datetime.time(17, 0)],
 [datetime.time(17, 0), datetime.time(0, 0)]]

1zip method inspired by Pierre-GM's solution above.

FYI, I prefer either my initial answer or Pierre's over this.


Edit/Update for 2nd part of the question: To do the timedeltas in one expression also (but I see no good reason why this shouldn't just be split out into different statements instead...):

>>> [[i, (datetime.datetime(101, 1, 1, j.hour, j.minute, j.second) -
...       datetime.timedelta(seconds=1)
...      ).time()
...  ] for i,j in zip(a, a[1:]+[a[0]])
... ]
[[datetime.time(0, 0), datetime.time(7, 59, 59)],
 [datetime.time(8, 0), datetime.time(12, 59, 59)],
 [datetime.time(13, 0), datetime.time(16, 59, 59)],
 [datetime.time(17, 0), datetime.time(23, 59, 59)]]

How about a list comprehension if not a 'simple loop'? Like:

>>> b = [[a[x], a[x+1]] for x in range(0, len(a)-1)] + [[a[-1], a[0]]]
>>> b
[[datetime.time(0, 0), datetime.time(8, 0)],
 [datetime.time(8, 0), datetime.time(13, 0)],
 [datetime.time(13, 0), datetime.time(17, 0)],
 [datetime.time(17, 0), datetime.time(0, 0)]]