1

I'm trying to update a list of lists within a dictionary where I would like to remove any list (within the list of lists) that has an item that is smaller than whatever I'm comparing it with. For example, given the dict of list of lists below..

mydict = {3603172: [
    ['M2-HCg18', 12.00, 12.00, 9.00, '12 x 12 x 9'],
    ['M2-HCg18', 16.00, 14.00, 8.00, '16 x 14 x 8'],
    ['M2-HCg18', 16.00, 14.00, 14.00, '16 x 14 x 14'],
    ['M2-HCg18', 18.00, 18.00, 18.00, '18 x 18 x 18'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ['M2-HCg18', 7.25, 4.75, 1.00, 'Bubble Mailer #4 7.25 x 5'],
    ['M2-HCg18', 9.25, 7.25, 1.00, 'Bubble Mailer DVD 10.25 x 7.25'],
    ['M2-HCg18', 28.00, 14.00, 8.00, '28x14x8'],
    ['M2-HCg18', 28.00, 14.00, 0.25, '28x14x4'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ]}

The above dict has a key (3603172) whose value is a list of lists. Note that there may be duplicate lists within the list of lists. What I'd like to do is compare the 4th item in any one of those inner lists with a given value and remove the list if the 3rd item in the list is smaller. So if my value is 0.5, I'd like to remove all the inner lists that are less than 0.5 at index 3.

I know that I can use mydict.itervalues() and operate on the value which is the list of lists and then run a compare on index 3 using an if statement with 0.5 but I don't really know a clean way of removing the list if the value at index 3 is smaller than 0.5. In the example above, I'd like to remove all lists with 0.25 at index 3 (smaller than 0.5). What would be a good way to do this?

user2993021
  • 87
  • 1
  • 8

3 Answers3

0

The concept of python list filtering is demonstrated in the following post: List filtering: list comprehension vs. lambda + filter

That is, you can iterate over the keys, and for each key, set its new value to be its old value filtered using one of the offered methods.

Community
  • 1
  • 1
Roy Shahaf
  • 494
  • 5
  • 10
0

Use dict comprehension.

>>> mydict = {3603172: [
    ['M2-HCg18', 12.00, 12.00, 9.00, '12 x 12 x 9'],
    ['M2-HCg18', 16.00, 14.00, 8.00, '16 x 14 x 8'],
    ['M2-HCg18', 16.00, 14.00, 14.00, '16 x 14 x 14'],
    ['M2-HCg18', 18.00, 18.00, 18.00, '18 x 18 x 18'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ['M2-HCg18', 7.25, 4.75, 1.00, 'Bubble Mailer #4 7.25 x 5'],
    ['M2-HCg18', 9.25, 7.25, 1.00, 'Bubble Mailer DVD 10.25 x 7.25'],
    ['M2-HCg18', 28.00, 14.00, 8.00, '28x14x8'],
    ['M2-HCg18', 28.00, 14.00, 0.25, '28x14x4'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ]}
>>> {i:[x for x in j if x[3] > 0.5] for i,j in mydict.items()}
{3603172: [['M2-HCg18', 12.0, 12.0, 9.0, '12 x 12 x 9'], ['M2-HCg18', 16.0, 14.0, 8.0, '16 x 14 x 8'], ['M2-HCg18', 16.0, 14.0, 14.0, '16 x 14 x 14'], ['M2-HCg18', 18.0, 18.0, 18.0, '18 x 18 x 18'], ['M2-HCg18', 7.25, 4.75, 1.0, 'Bubble Mailer #4 7.25 x 5'], ['M2-HCg18', 9.25, 7.25, 1.0, 'Bubble Mailer DVD 10.25 x 7.25'], ['M2-HCg18', 28.0, 14.0, 8.0, '28x14x8']]}
>>> pprint({i:[x for x in j if x[3] > 0.5] for i,j in mydict.items()})
{3603172: [['M2-HCg18', 12.0, 12.0, 9.0, '12 x 12 x 9'],
           ['M2-HCg18', 16.0, 14.0, 8.0, '16 x 14 x 8'],
           ['M2-HCg18', 16.0, 14.0, 14.0, '16 x 14 x 14'],
           ['M2-HCg18', 18.0, 18.0, 18.0, '18 x 18 x 18'],
           ['M2-HCg18', 7.25, 4.75, 1.0, 'Bubble Mailer #4 7.25 x 5'],
           ['M2-HCg18', 9.25, 7.25, 1.0, 'Bubble Mailer DVD 10.25 x 7.25'],
           ['M2-HCg18', 28.0, 14.0, 8.0, '28x14x8']]}
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

Iterate over the list of lists with reversed and remove the lists from the original dicts

k = mydict.keys()[0]
for  val in reversed(mydict[k]):
    if val[3]  < .5:
        mydict[k].remove(val)
print(mydict)

If you have more than one key:

for  v in mydict.itervalues():
    for val in reversed(v):
        if val[3] < .5:
            v.remove(val)
print(mydict)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321