pauldx
pauldx

Reputation: 1014

Convert Strings inside Iterator to Python list

I have a start date and an end date and I want them to appear as string values inside a list.

Example

datebetween = `["2020-01-01" , "2020-01-02" , "2020-01-03" ... ]

...

I wrote the code below to form an iterator and to get the dates but how can I convert those dates to final as list :

import datetime
import pandas as pd

start = datetime.datetime.strptime("2020-02-02", "%Y-%m-%d")
end = datetime.datetime.strptime("2020-03-02", "%Y-%m-%d")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days+1)]

# print ("data gen" , date_generated)

for date in date_generated:
    var = date.strftime("%Y-%m-%d")
    print (var)

Showing as below :

2020-02-02
2020-02-03
2020-02-04
2020-02-05
2020-02-06
2020-02-07
2020-02-08
2020-02-09
2020-02-10
2020-02-11
2020-02-12
2020-02-13
2020-02-14

I want the final var to be like : ["2020-02-02" , "2020-02-03" , "2020-02-04" .... ]

Upvotes: 0

Views: 222

Answers (5)

Rahul P
Rahul P

Reputation: 2663

I thought I'd add this to the list of solutions.

import datetime
import pandas as pd

start = datetime.datetime.strptime("2020-02-02", "%Y-%m-%d")
end = datetime.datetime.strptime("2020-03-02", "%Y-%m-%d")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days+1)]

# print ("data gen" , date_generated)
result = []
for date in date_generated:
    result.append(str(date).split()[0])

print(result)

Output

['2020-02-02',
 '2020-02-03',
 '2020-02-04',
 '2020-02-05',
 '2020-02-06',
 '2020-02-07',
 '2020-02-08',
 '2020-02-09',
 '2020-02-10',
 '2020-02-11',
 '2020-02-12',
 '2020-02-13',
 '2020-02-14',
 '2020-02-15',
 '2020-02-16',
 '2020-02-17',
 '2020-02-18',
 '2020-02-19',
 '2020-02-20',
 '2020-02-21',
 '2020-02-22',
 '2020-02-23',
 '2020-02-24',
 '2020-02-25',
 '2020-02-26',
 '2020-02-27',
 '2020-02-28',
 '2020-02-29',
 '2020-03-01',
 '2020-03-02']

Upvotes: 0

Gurkiran Kaur
Gurkiran Kaur

Reputation: 131

import datetime
import pandas as pd

start = datetime.datetime.strptime("2020-02-02", "%Y-%m-%d")
end = datetime.datetime.strptime("2020-03-02", "%Y-%m-%d")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end- 
start).days+1)]

# print ("data gen" , date_generated)
datesList=[]
for date in date_generated:
    datesList.append(date.strftime("%Y-%m-%d"))
print (datesList)

Upvotes: 0

Stephen Rauch
Stephen Rauch

Reputation: 49842

You can format the dates inside the comprehension like:

date_generated = [(start + datetime.timedelta(days=x)).strftime("%Y-%m-%d") 
                  for x in range(0, (end-start).days+1)]

Upvotes: 3

Vishakha Lall
Vishakha Lall

Reputation: 1324

Here's what you can do to achieve this, simply add the dates to a list after converting them to string.

import datetime
import pandas as pd

start = datetime.datetime.strptime("2020-02-02", "%Y-%m-%d")
end = datetime.datetime.strptime("2020-03-02", "%Y-%m-%d")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days+1)]

# print ("data gen" , date_generated)
var_list = list()
for date in date_generated:
    var = date.strftime("%Y-%m-%d")
    var_list.append(var)

Output of var_list

['2020-02-02',
 '2020-02-03',
 '2020-02-04',
 '2020-02-05',
 '2020-02-06',
 '2020-02-07',
 '2020-02-08',
 '2020-02-09',
 '2020-02-10',
 '2020-02-11',
 '2020-02-12',
 '2020-02-13',
 '2020-02-14',
 '2020-02-15',
 '2020-02-16',
 '2020-02-17',
 '2020-02-18',
 '2020-02-19',
 '2020-02-20',
 '2020-02-21',
 '2020-02-22',
 '2020-02-23',
 '2020-02-24',
 '2020-02-25',
 '2020-02-26',
 '2020-02-27',
 '2020-02-28',
 '2020-02-29',
 '2020-03-01',
 '2020-03-02']

Upvotes: 2

Data_Is_Everything
Data_Is_Everything

Reputation: 2037

You could create a final list called

  storeFinalDates = []

and then in the for loop call and append the dates to it like this:

  storeFinalDates.append(str(var))
  print(storeFinalDates)

Hope that helps! :)

Upvotes: 1

Related Questions