monicak
monicak

Reputation: 57

removing elements from a list which contains dictionary in python

I have a python script which stores a dictionary inside a list variable 'data' :

[  
   {  
      'hp':'cdldfdbc07s001:10000',
      'n':'cdldfdbc07s001',
      'rsid':'cdl-dit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldfdbc07s002:10000',
      'n':'cdldfdbc07s002',
      'rsid':'cdl-dit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldfdbc07s001:11000',
      'n':'cdldfdbc07s001',
      'rsid':'cdl-fit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldfdbc07s002:11000',
      'n':'cdldfdbc07s002',
      'rsid':'cdl-fit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldvjassvp073:10000',
      'n':'cdldvjassva073',
      'rsid':'mobile',
      'v':'2.6.3'
   },
   {  
      'hp':'cdldvjassva072:10000',
      'n':'cdldvjassva072',
      'rsid':'mobile',
      'v':'2.6.3'
   },
   {  
      'hp':'cdldvjassva074:11002',
      'n':'cdldvjassva074',
      'rsid':'cache2',
      'v':'2.6.3'
   },
   {  
      'hp':'cdldfdbc05s002:11000',
      'n':'cdldfdbc05s002',
      'rsid':'dit2',
      'v':'3.0.9'
   }
]

I want to

1.iterate through all the elements of the list and remove the element which contains "jass" in 'hp'.

my code for this doesn't seem to be deleting the element , my len(data) returns me the same number as before.

s=0   
while s< len(data):
     for i in ((data[s]['hp'])):
            if "jass" in i:
                    data.remove(s)
            s += 1
print len(data)

expected result list :

[  
   {  
      'hp':'cdldfdbc07s001:10000',
      'n':'cdldfdbc07s001',
      'rsid':'cdl-dit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldfdbc07s002:10000',
      'n':'cdldfdbc07s002',
      'rsid':'cdl-dit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldfdbc07s001:11000',
      'n':'cdldfdbc07s001',
      'rsid':'cdl-fit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldfdbc07s002:11000',
      'n':'cdldfdbc07s002',
      'rsid':'cdl-fit1',
      'v':'3.0.12'
   },
   {  
      'hp':'cdldfdbc05s002:11000',
      'n':'cdldfdbc05s002',
      'rsid':'dit2',
      'v':'3.0.9'
   }
]

Upvotes: 1

Views: 80

Answers (5)

paleolimbot
paleolimbot

Reputation: 436

If you go positive (think about which items to keep) rather than negative (remove certain items), you can do it using a list comprehension:

[item for item in data if "jass" not in item["hp"]]

Upvotes: 0

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140286

The best way in those filtering problems is to use a list comprehension to rebuild the list of dicts without jass in the values:

data = [{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldvjassvp073:10000', 'n': 'cdldvjassva073', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva072:10000', 'n': 'cdldvjassva072', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva074:11002', 'n': 'cdldvjassva074', 'rsid': 'cache2', 'v': '2.6.3'},
 {'hp': 'cdldfdbc05s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]

data_without_jass = [d for d in data if all("jass" not in v for v in d.values())]

print(data_without_jass)

result:

[{'hp': 'cdldfdbc07s001:10000', 'rsid': 'cdl-dit1', 'v': '3.0.12', 
'n': 'cdldfdbc07s001'}, {'hp': 'cdldfdbc07s002:10000', 'rsid': 'cdl-dit1', 'v': '3.0.12', 'n': 'cdldfdbc07s002'}, {'hp': 'cdldfdbc07s001:11000',
 'rsid': 'cdl-fit1', 'v': '3.0.12', 'n': 'cdldfdbc07s001'}, {'hp': 'cdldfdbc07s002:11000', 'rsid': 'cdl-fit1', 'v': '3.0.12', 'n': 'cdldfdbc07s002'}, 
{'hp': 'cdldfdbc05s002:11000', 'rsid': 'dit2', 'v': '3.0.9', 'n': 'cdldfdbc05s002'}]

the key here is the condition:

all("jass" not in v for v in d.values())

which tests if all values of each sub-dict doesn't contain "jass" (or with inverted logic: tests if no value of each sub-dict contains "jass")

I now notice that the condition only checks for hp key, so:

[d for d in data if "jass" not in d["hp"]]

is more accurate

Upvotes: 3

Patrick Artner
Patrick Artner

Reputation: 51683

You need to remove an item from your list, not items from a dict inside your list:

l = [{'hp': 'cdldfdbc07s001:10000',  'n': 'cdldfdbc07s001',  'rsid': 'cdl-dit1', 'v': '3.0.12'},  
     {'hp': 'cdldfdbc07s002:10000',  'n': 'cdldfdbc07s002',  'rsid': 'cdl-dit1', 'v': '3.0.12'},  
     {'hp': 'cdldfdbc07s001:11000',  'n': 'cdldfdbc07s001',  'rsid': 'cdl-fit1', 'v': '3.0.12'},  
     {'hp': 'cdldfdbc07s002:11000',  'n': 'cdldfdbc07s002',  'rsid': 'cdl-fit1', 'v': '3.0.12'}, 
     {'hp': 'cdldvjassvp073:10000',  'n': 'cdldvjassva073',  'rsid': 'mobile',   'v': '2.6.3'},  
     {'hp': 'cdldvjassva072:10000',  'n': 'cdldvjassva072',  'rsid': 'mobile',   'v': '2.6.3'},  
     {'hp': 'cdldvjassva074:11002',  'n': 'cdldvjassva074',  'rsid': 'cache2',   'v': '2.6.3'}, 
     {'hp': 'cdldfdbc05s002:11000',  'n': 'cdldfdbc05s002',  'rsid': 'dit2', 'v': '3.0.9'}
]

result = [x for x in l  if 'jass' not in x["hp"]] # the if condition lets only dicts in
                                                  # that do not have 'jass' in its key 'hp'

print (l)

print(result)

Output:

[{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, 
 {'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-di
t1', 'v': '3.0.12'}, 
 {'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, 
 {'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s0
02', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, 
 {'hp': 'cdldvjassvp073:10000', 'n': 'cdldvjassva073', 'rsid': 'mobile', 'v': '2.6.3'}, 
 {'hp': 'cdldvjassva072:10000', '
n': 'cdldvjassva072', 'rsid': 'mobile', 'v': '2.6.3'}, 
 {'hp': 'cdldvjassva074:11002', 'n': 'cdldvjassva074', 'rsid': 'cache2', 'v': '2.6.3'}, 
 {'hp': 'cdldfdbc05
s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]

[{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, 
 {'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-di
t1', 'v': '3.0.12'}, 
 {'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, 
 {'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s0
02', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, 
 {'hp': 'cdldfdbc05s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]

Upvotes: 0

JacobIRR
JacobIRR

Reputation: 8966

data = [{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-fit1', 'v': '3.0.12'},{'hp': 'cdldvjassvp073:10000', 'n': 'cdldvjassva073', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva072:10000', 'n': 'cdldvjassva072', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva074:11002', 'n': 'cdldvjassva074', 'rsid': 'cache2', 'v': '2.6.3'}, {'hp': 'cdldfdbc05s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]

Use remove to remove elements from a list:

print(len(data))  # prints 8


for d in data[:]:  # iterate over a COPY of the list[:]
    if 'jass' in d['hp']:
        data.remove(d)

 print(len(data))  # prints 5

You could also do this in a one-liner using list comprehension:

[data.remove(d) for d in data[:] if 'jass' in d['hp']]

Upvotes: 3

NoviceProgrammer123
NoviceProgrammer123

Reputation: 11

To delete python dictionary entries---

def removekey(d, key):
        r = dict(d)
        del r[key]
        return r

Upvotes: -2

Related Questions