Now I have a list:
TIME: ['15:10', '16:40', '16:50', '18:20', '18:30', '20:00']
I need to output using a loop like this:
1. 15:10 - 16:40
2. 16:50 - 18:20
3. 18:30 - 20:00
Now I have a list:
TIME: ['15:10', '16:40', '16:50', '18:20', '18:30', '20:00']
I need to output using a loop like this:
1. 15:10 - 16:40
2. 16:50 - 18:20
3. 18:30 - 20:00
A nice combination of enumarate
, zip
and slicing
values = ['15:10', '16:40', '16:50', '18:20', '18:30', '20:00']
for i, (start, end) in enumerate(zip(values[::2], values[1::2]), 1):
print(f"{i}. {start} - {end}")
1. 15:10 - 16:40
2. 16:50 - 18:20
3. 18:30 - 20:00
Try this:
TIME= ['15:10', '16:40', '16:50', '18:20', '18:30', '20:00']
for i in range(len(TIME)-1,2):
print(f'{i+1}. {TIME[i]} - {TIME[i+1]}')
You can use enumerate
, zip
, and iterate the corresponding items and print the values.
for i,v in enumerate(zip(TIME[::2], TIME[1::2]), 1):
print(f'{i}. {" - ".join(v)}')
1. 15:10 - 16:40
2. 16:50 - 18:20
3. 18:30 - 20:00