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>

<p>I took a simpler approach:</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}]

maxWeight = 610
shelves = []
shelf = []
current_shelf_weight = 0

for item in ListDictItem:
    if current_shelf_weight + item['Weight'] &lt;= maxWeight:
        shelf.append(item)
        current_shelf_weight += item['Weight']
    else:
        shelves.append(shelf)
        if item['Weight'] &lt;= maxWeight:
            shelf = [item]
            current_shelf_weight = item['Weight']
        else:
            shelf = []
            current_shelf_weight = 0

if shelf: #append any remaining items
    shelves.append(shelf)

print("shelves = " + str(shelves))
</code></pre>

<p>When I check the output of this code, I get this:</p>

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

<p>Which is exactly what you wanted.</p>

python list python-3.x dictionary