3

I know that assigning some values to an array would be as below:

arrayX = [20080711, 20080712, 20080713, 20080714]

But I couldn't find out how to quickly assign these values as a range to the array.

2
  • 2
    The "array" is called list in Python. Commented Dec 12, 2012 at 10:54
  • 1
    arrayX = []. for i in range(20080711, 20080714): arrayX.append(i) This will result [20080711, 20080712, 20080713, 20080714]. Commented Dec 12, 2012 at 13:36

1 Answer 1

6

In Python 2.x:

arrayX = range(20080711, 20080714+1)

in Python 3.x:

arrayX = list(range(20080711, 20080714+1))

However, if your ints represent something like a date (YYYYMMDD), it will be trickier:

from datetime import datetime, timedelta
arrayX = []
dt = datetime(2008, 7, 11)
while dt <= datetime(2008, 7, 14):
    arrayX.append(int(dt.strftime('%Y%m%d')))
    dt += timedelta(days=1)

which works over months and years.

Sign up to request clarification or add additional context in comments.

1 Comment

Keen observation, Eumiro! Does the date code you wrote above count for leap years?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.