1

I have the following list:

[0.12458333333051996, 0.7976157407465507, 0.4842939814843703, 0.9736921296280343, 0.4590740740750334, 0.48178240740526235, 0.46826388888439396, 0.0013078703705105, 0.4625000000014552, 0.4758796296300716, 0.4797685185185401, 6.87130787037313, 0.49761574074364034, 2.9876041666648234, 0.4683564814768033, 0.45549768517958, 0.43494212962832535, 2.030381944445253, 0.48494212963123573, 0.817592592597066]

I run this code on the list above:

for dt in dtime_list:
    if dt > 0.00694444444444444:
        dtime_list.remove(dt)

print(dtime_list)

Output:

==>[0.7976157407465507, 0.9736921296280343, 0.48178240740526235, 0.0013078703705105, 0.4758796296300716, 6.87130787037313, 2.9876041666648234, 0.45549768517958, 2.030381944445253, 0.817592592597066]

I was expecting there wouldn't remain a float greater than 0.00694444444444444. Why is this happening and how can I fix it?

My array is actually float values read from excel for time, with if "dt > 0.00694444444444444" I am trying to find the values greater than 10 mins and remove them from the list.

Mavlana
  • 13
  • 3

2 Answers2

0

As mentioned in the comments, you should not modify a list while you are iterating over it. Creating a new list containing only the elements that satisfy the criterion you want is easy with a list comprehension:

cutoff = 0.00694444444444444
filtered_list = [ dt for dt in dtime_list if dt <= cutoff ]
Pascal Bugnion
  • 4,878
  • 1
  • 24
  • 29
0

It's a very very bad idea to remove item within a while (or a for). Best make a new list!
nv_lst = []
for dt in dtime_list:
if dt <= 0.00694444444444444:
nv_lst.append(dt)
print(nv_lst)

Clodion
  • 1,017
  • 6
  • 12