0

I have a list of dates, i.e.:

dates = 1104, 1504, 1704, 2204

How do I transform it to

dates = 11-04, 15-04, 17-04, 22-04

with a for loop?

I know how to add more characters to the end of each element. Just not sure how to add it to the middle. I am using python 3.x

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user2803490
  • 39
  • 1
  • 8

3 Answers3

4

First, you have to convert all of the dates to strings (if they already are strings, you can skip this step):

dates = [1104, 1504, 1704, 2204]
dates = list(map(str, dates))

You could also iterate through the dates list and make them each into strings individually but this way does it all in one go.

Next you have to iterate through the list and add a hyphen in the middle of each date string, and then append the new string to a list. You insert the hyphen by separating the string and then making a new string.

datesSeparated = []
for i in dates:
    date = i[:2] + "-" + i[2:]
    datesSeparated.append(date)

You could also do this using a list comprehension if that's more your style:

datesSeparated = [i[:2] + "-" + i[2:] for i in dates]
Rilabeast
  • 60
  • 4
  • This is very good, thank you. But if I try to input the dates with input, I get something like choose dates: 1104, 1504 ['1-', '1-', '0-', '4-', ',-', ' -', '1-', '5-', '0-', '4-'] – user2803490 May 04 '20 at 15:02
  • 1
    Since `map(str, dates)` will give you a generator, you don't need to cast `list()` when iterating it. – RoadRunner May 04 '20 at 15:15
  • @RoadRunner I didnt know that, thanks for telling me – Rilabeast May 05 '20 at 07:07
2

I don't think you need datetime for this simple task.

You can use string slicing here to return of tuple of strings in "DD-MM" format. You can slice everything up until the 2nd character(but not including) with [:2], and everything after the second character with [2:]. Then you can just put a dash "-" between the slices.

>>> dates = 1104, 1504, 1704, 2204
>>> tuple(f"{x[:2]}-{x[2:]}" for x in map(str, dates))
('11-04', '15-04', '17-04', '22-04')

I returned a tuple since dates is that type. I also converted the integers from dates to str using map, since you can't slice integers. You can also use f-strings to format the slices into "DD-MM" format.

Also I'm assuming you wanted your expected output to be a tuple of strings, since dates = 11-04, 15-04, 17-04, 22-04 alone would be invalid.

Update

If you want to extract the dates from input(), you pass in input delimited by "," to split into a list of strings:

>>> dates = input()
1104, 1504, 1704, 2204
>>> dates
'1104, 1504, 1704, 2204'
>>> tuple(f"{x[:2]}-{x[2:]}" for x in map(str.strip, dates.split(",")))
('11-04', '15-04', '17-04', '22-04')

Which also strips leftover whitespace with str.strip, since inputs could possibly be 1104, 1504, 1704, 2204, 1104,1504,1704,2204, 1104, 1504,1704, 2204 etc. Its much easier to just split on ",", then strip the whitespace afterwards.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
2

Slicing in a readable way

dates = ["1104", "1504", "1704", "2204"]
new_dates = [date[:2] + "-" + date[2:] for date in dates]
print(new_dates)

['11-04', '15-04', '17-04', '22-04']

Daniel Poh
  • 129
  • 1
  • 10