Removing List from List of Lists with condition
<p>I have this list of projects, and I want to remove it one by one start from last item to item-n until it reach some total value of budget = 325.000</p>

<pre><code>from collections import namedtuple

Item = namedtuple('Item', 'region sector name budget target performance'.split())

sorted_KP = [Item(region='H', sector='2', name='H3', budget=7000.0, target=1.0, performance=4.0),
Item(region='H', sector='2', name='H10', budget=35000.0, target=15.0, performance=1.0),
Item(region='I', sector='2', name='I6', budget=50000.0, target=5.0, performance=0.40598931548848194),
Item(region='E', sector='4', name='E5', budget=75000.0, target=30.0, performance=0.0663966081766),
Item(region='C', sector='1', name='C1', budget=75000.0, target=50.0, performance=0.0308067750379),
Item(region='C', sector='1', name='C2', budget=75000.0, target=50.0, performance=0.0308067750379),
Item(region='C', sector='5', name='C4', budget=75000.0, target=50.0, performance=0.0308067750379),
Item(region='I', sector='2', name='I5', budget=100000.0, target=5.0, performance=0.40598931548848194),
Item(region='E', sector='4', name='E1', budget=100000.0, target=30.0, performance=0.0663966081766),
Item(region='D', sector='5', name='D21', budget=60000.0, target=4.0, performance=0.2479775110248),
Item(region='D', sector='5', name='D30', budget=10000.0, target=1.0, performance=0.1653183406832),
Item(region='D', sector='1', name='D23', budget=30000.0, target=20.0, performance=0.023659703723372342),
Item(region='C', sector='5', name='C3', budget=150000.0, target=75.0, performance=0.0308067750379),
Item(region='D', sector='5', name='D20', budget=30000.0, target=5.0, performance=0.0826591703416),
Item(region='H', sector='2', name='H6', budget=310576.0, target=1.0, performance=4.0),
Item(region='H', sector='3', name='H5', budget=9500.0, target=1.0, performance=0.1172008400616),
Item(region='E', sector='6', name='E3', budget=100000.0, target=30.0, performance=0.03747318294316411),
Item(region='G', sector='3', name='G17', budget=75000.0, target=20.0, performance=0.04132095963602382),
Item(region='C', sector='4', name='C5', budget=75000.0, target=25.0, performance=0.0308067750379),
Item(region='C', sector='2', name='C6', budget=30000.0, target=5.0, performance=0.0616135500758),
Item(region='C', sector='2', name='C7', budget=30000.0, target=5.0, performance=0.0616135500758),
Item(region='D', sector='6', name='D22', budget=65190.0, target=30.0, performance=0.020332158889648923),
Item(region='D', sector='5', name='D3', budget=100000.0, target=20.0, performance=0.0413295851708),
Item(region='D', sector='5', name='D4', budget=100000.0, target=20.0, performance=0.0413295851708),
Item(region='A', sector='1', name='A12', budget=25000.0, target=25.0, performance=0.00749432996938),
Item(region='A', sector='1', name='A13', budget=25000.0, target=25.0, performance=0.00749432996938),
Item(region='A', sector='3', name='A25', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='5', name='A26', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='1', name='A27', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='1', name='A29', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='3', name='A30', budget=4500.0, target=1.0, performance=0.02997731987752)]
</code></pre>

<p>But beside the total value, I have two others conditions of the item whether it should be removed or not.</p>

<p>First, after the item is removed there still remain at least one item in the list of lists that represent the same <em>region</em></p>

<p>Second, after the item is removed there still remain at least one item that represent the same <em>sector</em></p>

<p>For example, I can remove the last item because it represent region "A" and there left 5 items that also represent region "A". Also it represent sector "3" and there left 3 items that represent sector "3".</p>

<p>This removal and checking repeated until I reach total budget of removal at least 325.000</p>

<p>I did this code, but I can not get what I need. Please help me to correct it.</p>

<pre><code>from collections import Counter
unpack = []
for item in sorted_KP:
    item_budget = item[3]
    sum_unpack = sum(item[3] for item in unpack)
    budget = 325000

    remaining = []
    for item in sorted_KP:
         if item not in unpack:
             remaining.append(item)

    region_el = [item[0] for item in remaining]
    counter_R_el = Counter(region_el)

    sector_el = [item[1] for item in remaining]
    counter_S_el = Counter(sector_el)

    if counter_R_el &gt;= 1 or counter_S_el &gt;= 1:
        if sum_unpack &lt;= budget:
            unpack.append(item)

for item in unpack:
    print "\t", item
</code></pre>

<p>Here is what I got with my code, item-25 is still removed when it should not:</p>

<pre><code>unpack =Item(region='A', sector='3', name='A30', budget=4500.0, target=1.0, performance=0.02997731987752)
    Item(region='A', sector='1', name='A29', budget=4500.0, target=1.0, performance=0.02997731987752)
    Item(region='A', sector='1', name='A27', budget=4500.0, target=1.0, performance=0.02997731987752)
    Item(region='A', sector='5', name='A26', budget=4500.0, target=1.0, performance=0.02997731987752)
    Item(region='A', sector='3', name='A25', budget=4500.0, target=1.0, performance=0.02997731987752)
    Item(region='A', sector='1', name='A13', budget=25000.0, target=25.0, performance=0.00749432996938)
    Item(region='A', sector='1', name='A12', budget=25000.0, target=25.0, performance=0.00749432996938)
    Item(region='D', sector='5', name='D4', budget=100000.0, target=20.0, performance=0.0413295851708)
    Item(region='D', sector='5', name='D3', budget=100000.0, target=20.0, performance=0.0413295851708)
    Item(region='D', sector='6', name='D22', budget=65190.0, target=30.0, performance=0.020332158889648923)
</code></pre>

<p>Item-25 (project name: "A12") can not be removed even though we still have budget to be removed, because if it was remove, there'll be no more item represent region "A", and so on.</p>

<p>While the solution should be:</p>

<pre><code>unpack = [Item(region='A', sector='3', name='A30', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='1', name='A29', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='1', name='A27', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='5', name='A26', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='3', name='A25', budget=4500.0, target=1.0, performance=0.02997731987752),
Item(region='A', sector='1', name='A13', budget=25000.0, target=25.0, performance=0.00749432996938),
Item(region='D', sector='5', name='D4', budget=100000.0, target=20.0, performance=0.0413295851708),
Item(region='D', sector='5', name='D3', budget=100000.0, target=20.0, performance=0.0413295851708),
Item(region='D', sector='6', name='D22', budget=65190.0, target=30.0, performance=0.020332158889648923),
Item(region='C', sector='2', name='C7', budget=30000.0, target=5.0, performance=0.0616135500758)]
</code></pre>

<p>Thank you in advance for your help</p>

<p>I couldn't easily sort through your code.  <code>if counter_R_el &gt;= 1 or counter_S_el &gt;= 1:</code> just can't work, you can't compare a Counter() to an int().  It also looks like you continually remake/reset <em>things</em> in your loop suite and it became confusing.</p>

<p>You seem to be on the right track.</p>

<ul>
<li>To keep track of the number of regions and sectors collections.Counter() is a good choice.</li>
<li>look at each item in the list, and check if it meets your constraints</li>
<li>if removed, update the counters and the total cost</li>
</ul>

<p>Here is what I came up with:</p>

<pre><code>import collections, operator
Item = collections.namedtuple('Item', ['region', 'sector', 'name',
                                       'budget', 'target', 'performance'])

a = [Item(region='H', sector='2', name='H3', budget=7000.0, target=1.0, performance=4.0),
     Item(region='H', sector='2', name='H10', budget=35000.0, target=15.0, performance=1.0),
     Item(region='I', sector='2', name='I6', budget=50000.0, target=5.0, performance=0.40598931548848194),
     .....]

budget = 325000

# sort highest to lowest cost
a.sort(key = operator.attrgetter('budget'), reverse = True)
project_cost = sum(item.budget for item in a)
# constraint counters
region_count = collections.Counter(item.region for item in a)
sector_count = collections.Counter(item.sector for item in a)

# iterate over a copy of the list
b = a.copy()
for item in b:
    if project_cost &lt;= budget:
        break
    # check item against constraints
    if region_count[item.region] &gt; 1 and sector_count[item.sector] &gt; 1:
        # remove the item from the original list 
        a.remove(item)
        # uodate the counters and cost
        region_count[item.region] -= 1
        sector_count[item.sector] -= 1
        project_cost -= item.budget
</code></pre>

<p><code>a</code> contains the items that meet the constraints and have a total cost less than the budget.</p>

python list condition