0

I have a data set that looks like:

data_minimas=[(660, 0.0), (680, 0.0), (710, 0.0), (712, 0.0), (715, 0.0), (717, 0.0), (754, 0.0), 
(761, 0.0), (771, 0.0), (791, 0.0), (672, 3.2426156657036575e-09), (670, 5.621795915291507e-09), 
(667, 6.999418784280287e-09), (638, 8.311660795399324e-09), (636, 1.021511167383812e-08), 
(631, 1.03222580486856e-08), (634, 1.0889880297950812e-08), (757, 1.6140627900857082e-08), 
(733, 1.7310942425399663e-08), (785, 1.9027900568574484e-08), (729, 2.246316334112827e-08), 
(722, 2.5603465440057234e-08), (725, 2.6043967762809732e-08)]

Its a list of tuples. I need to remove the tuples that have a zero value. This is always the second value in the pair. Theoretically I thought this was easy, but I havent gotten anywhere yet. Heres what Ive tried:

for i in range(0,len(data_minimas)):
    if data_minimas[i][1]==0.0:
        del(data_minimas[i][1])
    else:
        pass

But this just removes the 0.0 values, and i need it to remove the whole tuple. if i try:

for i in range(0,len(data_minimas)):
    if data_minimas[i][1]==0.0:
        del(data_minimas[i])
    else:
        pass

which i thought would work, i get an index error. what am i doing wrong?

Learn4life
  • 227
  • 1
  • 8
  • Does this answer your question? [strange result when removing item from a list](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list) – wjandrea Aug 07 '20 at 16:50
  • Look up any Python tutorial on list sorting and filtering. There are several useful techniques. A "positive-pass" list comprehension is the most common. You can see an example in the oldest surviving answer below. – Prune Aug 07 '20 at 17:08

1 Answers1

1
>>> data_minimas = [i for i in data_minimas if i[1] != 0.0]
>>> data_minimas
[(672, 3.2426156657036575e-09),
 (670, 5.621795915291507e-09),
 (667, 6.999418784280287e-09),
 (638, 8.311660795399324e-09),
 (636, 1.021511167383812e-08),
 (631, 1.03222580486856e-08),
 (634, 1.0889880297950812e-08),
 (757, 1.6140627900857082e-08),
 (733, 1.7310942425399663e-08),
 (785, 1.9027900568574484e-08),
 (729, 2.246316334112827e-08),
 (722, 2.5603465440057234e-08),
 (725, 2.6043967762809732e-08)]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218