Is it possible to rearrange my list so that my list indexes matches the hour of the day?

Question:

I have a list of 24 prices between each hour of the day and night.

example:

Pricelist = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

I have also imported time that returns the current time: eg. N = 13.
I want to rearrange the Pricelist so that PriceList[N] gets put at index 0 in the list, and the next price gets index N+1.

The goal is to get a following list if N = 13:

Pricelist = [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Asked By: Larsy

||

Answers:

You can use list slicing and concatenation to achieve it:

Pricelist = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
N = 13

Pricelist_new = Pricelist[N-1:] + Pricelist[:N-1]
# [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Answered By: ILS

So you need that N needs to be equal to the hour of the day?
Have you tried

from datetime import datetime
currentDateAndTime = datetime.now()

N=currentDateAndTime.hour

This will make that N equals to the local hour

Answered By: A programmer
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.