Create a list from a list of dictionaries subject to a condition (counter and accumulator) in python
<p>I have a list dictionary of items: </p>

<pre><code>ListDictItem = [ {'Item No': 1,'Weight':610,'Quantity':2},{'Item No': 2,'Weight':610,'Quantity':2},{'Item No': 3,'Weight':500,'Quantity':2},{'Item No': 4,'Weight':484,'Quantity':2},{'Item No': 5,'Weight':470,'Quantity':2},{'Item No': 6,'Weight':440,'Quantity':2},{'Item No': 7,'Weight':440,'Quantity':2},{'Item No': 8,'Weight':400,'Quantity':2}] 
</code></pre>

<p>I have created a list of weights from above like this: </p>

<pre><code>ItemWeigths: [610.0, 610.0, 500.0, 484.0, 470.0, 440.0,440, 400.0] 
</code></pre>

<p>I would like to pack the items in shelves such that the total weight of each shelf is less than a particular value and have a list in the following format:</p>

<pre><code>shelves = [[{'Item No': 1,'Weight':610,'Quantity':2}],[{'Item No': 2,'Weight':610,'Quantity':2}],[{'Item No': 3,'Weight':500,'Quantity':2}],[{'Item No': 4,'Weight':484,'Quantity':2}], [{'Item No': 7,'Weight':440,'Quantity':2}],[{'Item No': 8,'Weight':400,'Quantity':2}] ]
</code></pre>

<p>where the sum of the all the weights of a particular shelf is &lt;= 610</p>

<p>I have created a code that it is packing the first shelf correctly but I am unable to write the code for creating shelves for other items:</p>

<pre><code>shelves=[] 
ShelvesWeightTotal = 0
shelf=[] 
new=[] 
ShelfToPack=[] 

 for i in range(0,len(ItemWeigths)): 
    while ShelvesWeightTotal + ItemWeigths[i]   &lt;= WeightOfObject: 
      shelves += [ItemWeigths[i]] 
      ShelvesWeightTotal = sum(shelves)
</code></pre>

<p>The above code works for 1st item but to create other shelves for remaining items I wrote the code which doesnot work:</p>

<pre><code>for i in range(0,len(ItemWeigths)):
    while ItemsToCut !=[]:
        if ShelvesWeightTotal + ItemWeigths[i]   &lt;=WeightOfObject: 
           shelves += [ItemWeigths[i]] 
           ShelvesWeightTotal = sum(shelves)
        ItemWeigths.pop(i)
        ItemsToCut.pop(i)
shelf.append(shelves)
</code></pre>

<p>Any suggestion to write the code to create shelves in a better way will be appreciated.</p>

<pre><code>ListDictItem = [ {'Item No': 1,'Weight':610,'Quantity':2},{'Item No': 2,'Weight':610,'Quantity':2},{'Item No': 3,'Weight':500,'Quantity':2},{'Item No': 4,'Weight':484,'Quantity':2},{'Item No': 5,'Weight':470,'Quantity':2},{'Item No': 6,'Weight':440,'Quantity':2},{'Item No': 7,'Weight':440,'Quantity':2},{'Item No': 8,'Weight':400,'Quantity':2}];

sh = []; #weight, itemNo
for a in ListDictItem:
    for b in range(0, a['Quantity']):
        for j in range(0, len(sh)):
            if sh[j]['weight'] + a['Weight'] &lt;= 610:
                sh[j]['weight'] += a['Weight']
                sh[j]['items'].append(a['Item No'])
    else:
        sh.append({'weight' : a['Weight'], 'items' : [a['Item No']]})
print(sh)
</code></pre>

<p>A very simple solution to your problem.</p>

python list python-3.x dictionary