0

I need help to iterate through a data dictionary column called 'TimeO' and 'TimeC' in this function.

Data_Dict = {'TimeO': ['9:00:00', '10:00:00'] 'TimeC': ['14:00:00', '16:00:00']}

x should be values from TimeO and y should be values from TimeC.

I can't figure out how to iterate the values

def timed_duration():
        opening = datetime.strptime(x, '%H:%M:%S')
        closing = datetime.strptime(y, '%H:%M:%S')
        sec =(closing-opening).total_seconds()
        hour = sec/3600
        return(hour)
timed_duration()

x and y should iterate through 400 records but I don't know what to do

Georgy
  • 12,464
  • 7
  • 65
  • 73
NinJesus
  • 17
  • 5
  • 1
    Possible duplicate of [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – Georgy Apr 08 '19 at 09:26

1 Answers1

0

Considering your data look like this:

Data_Dict = {'TimeO': ['9:00:00', '10:00:00'], 'TimeC': ['14:00:00', '16:00:00']}

def timed_duration(data_dict):
    hours = []  # create an empty list to store all the results
    for x, y in zip(data_dict['TimeO'], data_dict['TimeC']):
        opening = datetime.strptime(x, '%H:%M:%S')
        closing = datetime.strptime(y, '%H:%M:%S')
        sec =(closing-opening).total_seconds()
        hour = sec/3600
        hours.append(hour)
    return hours  # no parenthesis in return

timed_duration(Data_Dict)

This will create a list called hours filled with the results of your function. The zip() thingy allow you to iterate through both objects at the same time.

Plopp
  • 947
  • 8
  • 16