Sashaank
Sashaank

Reputation: 964

How to break loop if variable changes

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.

Upvotes: 0

Views: 3043

Answers (6)

alec_djinn
alec_djinn

Reputation: 10789

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

Upvotes: 0

Marek Maszay
Marek Maszay

Reputation: 1547

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)

Upvotes: 2

Amin Rashidbeigi
Amin Rashidbeigi

Reputation: 684

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

Upvotes: 2

Neo
Neo

Reputation: 3786

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"].

Upvotes: 1

Ardis thrash
Ardis thrash

Reputation: 164

This should work.

for time in lst:
    minute = time[3:5]
    if minute != lst[0][3:5]:
        break
    print minute

Upvotes: 4

Ahmad Farhan
Ahmad Farhan

Reputation: 605

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

Upvotes: 1

Related Questions