0

I am running a for loop, in which the iterator loops through list of time based values.

for example this is what my list looks like

lst = ['00:00:01', '00:00:05', '00:00:07', '00:01:10', ... , '00:20:23']

I want to check if the minute part of the time data changes. If it changes then break the loop.

this is my code so far

time_lst = []

for time in lst:
    minute = time[3:5]

    if minute changes:
        time_lst.append(time)
        break

print(time_lst)

in the above program, how do I program the if condition?

I looked online but I could not find a good solution to this problem.

5
  • 1
    assign a value to "minute" before the loop starts. during loop, check the "current" minute against the "previous" assigned minute. If they match, proceed. If they mismatch, break. Commented Aug 9, 2019 at 8:38
  • Essentially, you need a variable that keeps "state" of the minute value during iteration Commented Aug 9, 2019 at 8:38
  • Understood how to do this now. thank you Commented Aug 9, 2019 at 8:46
  • @Sashaank Please, mark an answer, if your issue is resolved. Commented Aug 9, 2019 at 8:53
  • Only not sure if you want keep tracking of times before change or after change as in ['00:00:07', '00:00:50' , '00:01:10'] if you want to get 00:00:50 or 00:01:10 in list @Sashaank Commented Aug 9, 2019 at 9:19

6 Answers 6

4

This should work.

for time in lst:
    minute = time[3:5]
    if minute != lst[0][3:5]:
        break
    print minute
Sign up to request clarification or add additional context in comments.

1 Comment

The only answer that, gives the solution to the question and it's from a new contributor
2

EDIT

With edited question so that it keeps changes of time between minutes

time_lst = []
lastMinute = lst[0][3:5] #so that it won't break on first item
for time in lst:
    minute = time[3:5]

    if minute != lastMinute: # `<>` still usable in python2, `!=` is better for compatibility between python2 and python3
        time_lst.append(time)
    lastMinute = minute

print(time_lst)

3 Comments

do you mean != ?
@BlueRineS <> can be used still in 2.7 but yeah for python3+ it should be !=
Great. Maybe you should specify that in your answer
2

You can simply do it by saving previous minute and compare it with new value:

previous_minute = lst[0][3:5]
for time in lst:
    minute = time[3:5]
    if previous_minute not minute:
        break
    previous_minute = minute

If you want to save changed times:

changed_times = []
previous_minute = lst[0][3:5]
for time in lst:
    minute = time[3:5]
    if previous_minute not minute:
        changed_times.append(time)
    previous_minute = minute

Comments

1

Save the first element's minute value before starting the loop. Compare all following values with it. break if it doesn't match.

prev=lst[0][3:5]
for time in lst:
    if(time[3:5]!=prev):
        break

Comments

1

You could keep a variable out of the loop, holding the first minute. Then the test is easy - you compare the new minute to the first one. Once they're inequal, it means the minute changed.

first_minute = lst[0][3:5]
for time in lst:
    minute = time[3:5]

    if minute != first_minute:
        break

However, if all you're doing in the loop is updating another list, you could use list comprehension to filter the non-minute-equal times:

new_list = [time in lst if time[3:5] == lst[0][3:5]]

You could also save lst[0][3:5] to a first_minute variable as I've done above. Another modification is to use filter and lambda t: t[3:5] == lst[0][3:5].

NOTICE: The list comprehension solution filters all the list. It means if after the minute change, if the times go back to the same minute value (maybe with a different hour value?), it will include them as well - while the loop & break solution won't. Use according to your needs.

To clarify, using the first solution with lst = ["00:00:00", "00:00:04", "00:01:00", "01:00:02"] will result in iterating over only ["00:00:00", "00:00:04"], while the second will give (for the same lst) ["00:00:00", "00:00:04", "01:00:02"].

Comments

0

You need to keep track of the last value.

There are several ways to accomplish this. The most straight forward is to update a variable.

last_value = False
for time in lst:
    minute = time[3:5]
    if not last_value:
        last_value = minute
    else:
        if minute != last_value:
            ...
            break

Comments

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.