ensnare
ensnare

Reputation: 42123

In python, a good way to remove a list from a list of dicts

I have a list of dicts:

list =  [{'title': u'Politics', 'id': 1L, 'title_url': u'Politics'}, 
         {'id': 3L, 'title_url': u'Test', 'title': u'Test'}]

I'd like to remove the list item with title = 'Test'

What is the best way to do this given that the order of the key/value pairs change?

Thanks.

Upvotes: 2

Views: 196

Answers (5)

Evgeny
Evgeny

Reputation: 3274

new_lst = filter(lambda x: 'title' in x and x['title']!=u'Test', lst)

Upvotes: 0

pycruft
pycruft

Reputation: 68815

More verbose than the above answers but modify the list in place rather than creating a copy of it (Have no idea which would be faster - copying might be the way to go anyway!)

lst =  [{'title': u'Politics', 'id': 1L, 'title_url': u'Politics'},  
    {'id': 3L, 'title_url': u'Test', 'title': u'Test'}]  
for i in xrange(len(lst)-1,0,-1):  
if lst[i].get("title")=="Test":  
    del lst[i]  

Modifies the list in place rather than copying it, copes with removing multiple dicts which have "title":"Test" in them and copes if there's no such dict.

Note that .get("title") return None if there's no matching key whereas ["title"] raises an exception.

If you could guarantee there would be just one matching item you could also use (and wanted to modify in place rather than copy)

for i,d in enumerate(lst):  
    if d.get("title")=="Test":  
        del lst[i]  
        break  

Probably simplest to stick with

[x for x in lst if x.get("title")!="Test"]

Upvotes: 3

Daniel Stutzbach
Daniel Stutzbach

Reputation: 76745

mylist = [x for x in mylist if x['title'] != 'Test']

Other solutions are possible, but any solution will be O(n) since you have to search through the whole list for the right element. Given that, go with the simplest approach.

Upvotes: 4

Ben James
Ben James

Reputation: 125307

L = [{'title': u'Politics', 'id': 1L, 'title_url': u'Politics'}, 
     {'id': 3L, 'title_url': u'Test', 'title': u'Test'}]
L = [d for d in L if d['title'] != u'Test']

Tips: The items in a dict aren't ordered anyway. Using the name of a built-in function like list as a variable name is a bad idea.

Upvotes: 3

SilentGhost
SilentGhost

Reputation: 319949

[i for i in lst if i['title']!= u'Test']

Also, please don't use list as a variable name, it shadows built-in.

Upvotes: 6

Related Questions