snipt

Ctrl+h for KB shortcuts

Text only

How to remove items from a list without getting the Value Error

1
2
3
4
5
6
7
8
9
cond1 = [223,232,22,1,23,2,64,9999.0,432,2,4,9999.0,9999]
delimiter = len(cond1) -1
cond1temp = []

for i in range(delimiter):
        if(cond1[i] != 9999):
            cond1temp.append(cond1[i])
cond1 = cond1temp
print "Cond1: ", cond1

Description

When you delete last items fom a list, Python gives Value Error, so instead of removing items, copy all items you want to a temp list and then assign temp list to your original list.
https://snipt.net/embed/f34614855e9e3aac0c9a9540a1769b96/
/raw/f34614855e9e3aac0c9a9540a1769b96/
f34614855e9e3aac0c9a9540a1769b96
text
Text only
9
2019-07-20T12:04:23
True
False
False
/api/public/snipt/125876/
how-to-remove-items-from-a-list-without-getting-the-value-error
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a> <a href="#L-9">9</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>cond1 = [223,232,22,1,23,2,64,9999.0,432,2,4,9999.0,9999] </span><span id="L-2"><a name="L-2"></a>delimiter = len(cond1) -1 </span><span id="L-3"><a name="L-3"></a>cond1temp = [] </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a>for i in range(delimiter): </span><span id="L-6"><a name="L-6"></a> if(cond1[i] != 9999): </span><span id="L-7"><a name="L-7"></a> cond1temp.append(cond1[i]) </span><span id="L-8"><a name="L-8"></a>cond1 = cond1temp </span><span id="L-9"><a name="L-9"></a>print &quot;Cond1: &quot;, cond1 </span></pre></div> </td></tr></table>
list, lstats, python