Remove dictionary from list

If I have a list of dictionaries, say:

[{'id': 1, 'name': 'paul'},
 {'id': 2, 'name': 'john'}]

and I would like to remove the dictionary with id of 2 (or name 'john'), what is the most efficient way to go about this programmatically (that is to say, I don't know the index of the entry in the list so it can't simply be popped).

Asked by: Cherry448 | Posted: 30-11-2021

Answer 1

thelist[:] = [d for d in thelist if d.get('id') != 2]

Edit: as some doubts have been expressed in a comment about the performance of this code (some based on misunderstanding Python's performance characteristics, some on assuming beyond the given specs that there is exactly one dict in the list with a value of 2 for key 'id'), I wish to offer reassurance on this point.

On an old Linux box, measuring this code:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(99)]; import random" "thelist=list(lod); random.shuffle(thelist); thelist[:] = [d for d in thelist if d.get('id') != 2]"
10000 loops, best of 3: 82.3 usec per loop

of which about 57 microseconds for the random.shuffle (needed to ensure that the element to remove is not ALWAYS at the same spot;-) and 0.65 microseconds for the initial copy (whoever worries about performance impact of shallow copies of Python lists is most obviously out to lunch;-), needed to avoid altering the original list in the loop (so each leg of the loop does have something to delete;-).

When it is known that there is exactly one item to remove, it's possible to locate and remove it even more expeditiously:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(99)]; import random" "thelist=list(lod); random.shuffle(thelist); where=(i for i,d in enumerate(thelist) if d.get('id')==2).next(); del thelist[where]"
10000 loops, best of 3: 72.8 usec per loop

(use the next builtin rather than the .next method if you're on Python 2.6 or better, of course) -- but this code breaks down if the number of dicts that satisfy the removal condition is not exactly one. Generalizing this, we have:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*3; import random" "thelist=list(lod); where=[i for i,d in enumerate(thelist) if d.get('id')==2]; where.reverse()" "for i in where: del thelist[i]"
10000 loops, best of 3: 23.7 usec per loop

where the shuffling can be removed because there are already three equispaced dicts to remove, as we know. And the listcomp, unchanged, fares well:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*3; import random" "thelist=list(lod); thelist[:] = [d for d in thelist if d.get('id') != 2]"
10000 loops, best of 3: 23.8 usec per loop

totally neck and neck, with even just 3 elements of 99 to be removed. With longer lists and more repetitions, this holds even more of course:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*133; import random" "thelist=list(lod); where=[i for i,d in enumerate(thelist) if d.get('id')==2]; where.reverse()" "for i in where: del thelist[i]"
1000 loops, best of 3: 1.11 msec per loop
$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*133; import random" "thelist=list(lod); thelist[:] = [d for d in thelist if d.get('id') != 2]"
1000 loops, best of 3: 998 usec per loop

All in all, it's obviously not worth deploying the subtlety of making and reversing the list of indices to remove, vs the perfectly simple and obvious list comprehension, to possibly gain 100 nanoseconds in one small case -- and lose 113 microseconds in a larger one;-). Avoiding or criticizing simple, straightforward, and perfectly performance-adequate solutions (like list comprehensions for this general class of "remove some items from a list" problems) is a particularly nasty example of Knuth's and Hoare's well-known thesis that "premature optimization is the root of all evil in programming"!-)

Answered by: Kelsey587 | Posted: 01-01-2022

Answer 2

Here's a way to do it with a list comprehension (assuming you name your list 'foo'):

[x for x in foo if not (2 == x.get('id'))]

Substitute 'john' == x.get('name') or whatever as appropriate.

filter also works:

foo.filter(lambda x: x.get('id')!=2, foo)

And if you want a generator you can use itertools:

itertools.ifilter(lambda x: x.get('id')!=2, foo)

However, as of Python 3, filter will return an iterator anyway, so the list comprehension is really the best choice, as Alex suggested.

Answered by: Elise882 | Posted: 01-01-2022

Answer 3

# assume ls contains your list
for i in range(len(ls)):
    if ls[i]['id'] == 2:
        del ls[i]

Will probably be faster than the list comprehension methods on average because it doesn't traverse the whole list if it finds the item in question early on.

Answered by: Michelle764 | Posted: 01-01-2022

Answer 4

This is not properly an anwser (as I think you already have some quite good of them), but... have you considered of having a dictionary of <id>:<name> instead of a list of dictionaries?

Answered by: Miller292 | Posted: 01-01-2022

Answer 5

Supposed your python version is 3.6 or greater, and that you don't need the deleted item this would be less expensive...

If the dictionaries in the list are unique :

for i in range(len(dicts)):
    if dicts[i].get('id') == 2:
        del dicts[i]

If you want to remove all matched items :

for i in range(len(dicts)):
    if dicts[i].get('id') == 2:
        del dicts[i]

You can also to this to be sure getting id key won't raise keyerror regardless the python version

if dicts[i].get('id', None) == 2

Answered by: Joyce175 | Posted: 01-01-2022

Answer 6

You can try the following:

a = [{'id': 1, 'name': 'paul'},
     {'id': 2, 'name': 'john'}]

for e in range(len(a) - 1, -1, -1):
    if a[e]['id'] == 2:

If You can't pop from the beginning - pop from the end, it won't ruin the for loop.

Answered by: First Name540 | Posted: 01-01-2022

Answer 7

You could try something along the following lines:

def destructively_remove_if(predicate, list):
      for k in xrange(len(list)):
          if predicate(list[k]):
              del list[k]
      return list

  list = [
      { 'id': 1, 'name': 'John' },
      { 'id': 2, 'name': 'Karl' },
      { 'id': 3, 'name': 'Desdemona' } 

  print "Before:", list
  destructively_remove_if(lambda p: p["id"] == 2, list)
  print "After:", list

Unless you build something akin to an index over your data, I don't think that you can do better than doing a brute-force "table scan" over the entire list. If your data is sorted by the key you are using, you might be able to employ the bisect module to find the object you are looking for somewhat faster.

Answered by: Victoria986 | Posted: 01-01-2022

Similar questions

python - remove dictionary from list if exists

I am trying to remove a dictionary from a list if it already exists but it doesn't seem to be working. Can anyone see what I am doing wrong or advise me what I should be doing new_dict = {'value': 'some value', 'key': 'someKey'} if new_dict in my_list: my_list.remove(new_dict) new_list is a list of dictionaries where new_dict is definitely in

python - Remove values from dictionary based on key values

data = {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5'} keys = ('one', 'four') unwanted = set(keys) - set(data) for unwanted_key in unwanted: del data[unwanted_key] The output that I want is: data = {'two': '2', 'three': '3', 'five': '5'} What am I doing wrong? I am using the code of this accepted

python - User List to Remove keys from Dictionary

What is the best way to get a user to be able to enter a list(or singular) number to remove a string key entry from a dictionary? so far this is where I am. My dictionary to remove the values from(the dictionary is generated so will vary). d = {'Seven': 22, 'Six': 0, 'Three': 35, 'Two': 0, 'Four': 45, 'Five': 34, 'Eight': 0} 1) So how do I get the input is my first query. T...

dictionary - python remove keys whose values add up to a total

dict1 = {datetime.timedelta(3, 21388, 702926): 2, datetime.timedelta(10, 21388, 702926): 1, datetime.timedelta(3, 65011, 297074): 2, datetime.timedelta(14, 65011, 297074): 1, datetime.timedelta(17, 65011, 297074): 1, datetime.timedelta(0, 65011, 297074): 1, datetime.timedelta(7, 65011, 297074): 1, datetime.timedelta(10, 65011, 297074): 1, datetime.timedelta(0, 21388, 702926): 1} I am trying to rem...

python - Remove all lines containing word NOT in dictionary

I have a dictionary of english words from /usr/share/dict/words I have a huge file of sentences, line by line. I'm trying to remove these weird sentences with foreign and out of vocabulary words by comparing against the dictionary. Master.txt Thanks to Your Greatness (谢谢你的美好) Himatnagar has a small Railway Station Pu$haz Ink Can anyone help? I tried using

python - How to remove dictionary key with known value?

Assume python dict: mydict = {'a': 100, 'b': 200, 'c': 300} I know one of the values: value = 200 How to remove the 'b': 200 pair from the dict? I need this: mydict = {'a': 100, 'c': 300}

python - Add then remove in dictionary

This is my code def make_directory(): return [] def get_number(directory, person): a = dict(directory) if person in a: return a[person] else: return 0 def add_number(directory, person, number): directory.append((person,number)) return(str(person) + ' ' +'added!') def del_number(directory, person): a = dict(directory) if person in a: del a[...

Python remove keys with the same value on a dictionary

I need to do a not "natural" operation on a dictionary so i wondering what is the best pythonic way to do this. I need to simplify a dictionary by removing all the keys on it with the same value (keys are differents, values are the same) For example: Input: dict = {key1 : [1,2,3], key2: [1,2,6], key3: [1,2,3]} expected output: {key1 : [1,2,3], key2:...

python - Can I remove a single value from a dictionary?

I have a dictionary and I want to remove a single value from a key:values combination. For example, when having the following dictionary: d = {1: [2, 3, 4, 7], 2: [1, 3, 4]} I'd like to remove the key:value 1:2, so that the dictionary becomes: {1: [3, 4, 7], 2: [1, 3, 4]} Is this doable?

python - remove dictionary from list in pandas colum

I have dataframe where one of the columns is a list of dictionaries that is stored as a string like so: name age misc Jim 44 "[{"value":3,"type":"cars"},{"value":1,"type":"pets"},{"value":13,"type":"shoes"}]" Bob 25 "[{"value":3,"type":"siblings"},{"value":1,"type":"pets"}]" Sue 55 "[]" I want to get to this where each type becomes it's own column with the corresponding values: ...

Python - Loop Through List and Remove from Dictionary

My goal is to simply loop through a string, look up each character in the keys of a dictionary, and remove that key. This is what I have now: def m(z,word): for char in word: if char in z: z = z.pop(char) else: return False print True return z print m({'m':1,'y':2,'z':3},'my') I understand the error "argument of type 'int' is not i...

python - Remove space from dictionary keys

I have a dictionary with keys/values such as: {'C 14': ['15263808', '13210478'], 'W 1': ['13122205']} How can I remove the white space from all of the keys so they instead would be: {'C14': ['15263808', '13210478'], 'W1': ['13122205']}

python - Remove values from lists of values in a dictionary

I am using Python 2.7 with Windows 7. I have a dictionary and would like to remove values that correspond to (key, value) pairs from another dictionary. For example, I have a dictionary t_dict. I would like to remove corresponding (key,value) pairs that are in the dictionary values_to_remove so that I end up with dictionary final_dict t_dict = { 'a'...

Python - Remove Single Quotes From List Items When Adding To Dictionary

I have tried all the recommendations on similar posts, but have had no luck. I have a list of items that I'm using to create values for a list of dictionaries. That list ultimately becomes a JSON object, so I can't have the single quotes around each list item that I add to it. metrics = ["test_met_1","test_met_2","test_met_3","test_met_4","test_met_5"] data = { "reportDescription":{ "reportSuite...

python - Need to remove items from both a list and a dictionary of tuple value pairs at same time

This is very related to a previous question but I realised that my objective is much more complicated: I have a sentence: "Forbes Asia 200 Best Under 500 Billion 2011" I have tokens like: oldTokens = [u'Forbes', u'Asia', u'200', u'Best', u'Under', u'50...

python - Remove tuples from dictionary value list if first value in tuple = 1

First I have a dictionary d1 that looks like this: d1 = { 'w' : ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'], 'x' : ['d', 'd', 'd', 'e', 'e'], 'y' : ['f', 'f', 'g'], 'z' : ['h', 'i'] } Then I loop through that dictionary and make a new dictionary in which each value is a list with two elements: an integer and list. The integer is the number o...

python - Remove a char in string and then use dictionary to do key match

I want write a function to convert string to float by using map and reduce. (no int() here). Here is my code so far: from functools import reduce def str2float(s): def char2number(s): s = s.replace('.', '') return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] #here i try to get pure numbers def find_digt(s): return (len(s) - s.find('.'...

python - Remove dictionary values based on regex?

I have the following dictionary in Python dict1 = {"key1": 2345, "key2": 356, "key3": 773, "key44": 88, "key333": 12, "key3X": 13} I want to delete keys that do not follow the pattern "xxx#" or "xxx##". That is, three characters followed by a one-digit integer or a two-digit integer. Using the above example, this is: new_dict = {"key1": 2345, "key2...

python - Remove nested keys and move value to main dictionary keys

I have a problem when I try to merge two dictionaries to fit for doing a post later. For some reason the get seems to be nested and Im not sure how to clean it up. Would be great to get some tips on optimizing the code as well, right now it looks a bit messy. for network in networks: post_dict = {e1:e2 for e1,e2 in network['extattrs'].iteritems() if e1 not in keys } pprint (post...

python - Remove item with empty value in django dictionary

I want to remove dictionary item on my list. Animals = ['dog', 'cat', 'fish', 'goat'] Owner = ['Nash', 'Luffy', '', ''] C = dict(zip(Animals, Owner)) C = {'dog':'Nash', 'cat':'Luffy', 'fish':'', 'goat':''} What should I do to achieve the result below: C = {'dog':'Nash', 'cat':'Luffy'}

python - List all words in a dictionary that start with <user input>

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string? Ex: User: "abd" Program:abdicate, abdomen, abduct... Thanks! Edit: I'm using python, but I assume that this is a fairly language-independent problem.

python, dictionary and int error

I have a very frustrating python problem. In this code fixedKeyStringInAVar = "SomeKey" def myFunc(a, b): global sleepTime global fixedKeyStringInAVar varMe=int("15") sleepTime[fixedKeyStringInAVar] = varMe*60*1000 #more code Now this works. BUT sometimes when I run this function I get TypeError: 'int' object does not support item assignment

python - Best way to create a NumPy array from a dictionary?

I'm just starting with NumPy so I may be missing some core concepts... What's the best way to create a NumPy array from a dictionary whose values are lists? Something like this: d = { 1: [10,20,30] , 2: [50,60], 3: [100,200,300,400,500] } Should turn into something like: data = [ [10,20,30,?,?], [50,60,?,?,?], [100,200,300,400,500] ] ...

python - List a dictionary

In a list appending is possible. But how I achieve appending in dictionary? Symbols from __ctype_tab.o: Name Value Class Type Size Line Section __ctype |00000000| D | OBJECT|00000004| |.data __ctype_tab |00000000| r | OBJECT|00000101| |.rodata Symbols from _ashldi3.o: Name Value Class ...

python - How to filter a dictionary by value?

Newbie question here, so please bear with me. Let's say I have a dictionary looking like this: a = {"2323232838": ("first/dir", "hello.txt"), "2323221383": ("second/dir", "foo.txt"), "3434221": ("first/dir", "hello.txt"), "32232334": ("first/dir", "hello.txt"), "324234324": ("third/dir", "dog.txt")} I want all values that are equal to each other to be moved into...

Python and dictionary like object

I need a python 3.1 deep update function for dictionaries (a function that will recursively update child dictionaries that are inside a parent dictionary). But I think, in the future, my function could have to deal with objects that behave like dictionaries but aren't. And furthermore I want to avoid using isinstance and type (because they are considered b...

C# way to mimic Python Dictionary Syntax

Is there a good way in C# to mimic the following python syntax: mydict = {} mydict["bc"] = {} mydict["bc"]["de"] = "123"; # &lt;-- This line mydict["te"] = "5"; # &lt;-- While also allowing this line In other words, I'd like something with [] style access that can return either another dictionary or a string type, depending on how it has been set. I've been trying to work...

python - Can a dictionary be passed to django models on create?

Is it possible to do something similar to this with a list, dictionary or something else? data_dict = { 'title' : 'awesome title', 'body' : 'great body of text', } Model.objects.create(data_dict) Even better if I can extend it: Model.objects.create(data_dict, extra='hello', extra2='world')

python - Make Dictionary From 2 List

This question already has answers here:

Python dictionary simple way to add a new key value pair

Say you have, foo = 'bar' d = {'a-key':'a-value'} And you want d = {'a-key':'a-value','foo':'bar'} e = {'foo':foo} I know you can do, d['foo'] = foo #Either of the following for e e = {'foo':foo} e = dict(foo=foo) But, in all these way to add the variable foo to dict, I have had to use the word foo twice; onc...

sorting - In Python, how can you easily retrieve sorted items from a dictionary?

Dictionaries unlike lists are not ordered (and do not have the 'sort' attribute). Therefore, you can not rely on getting the items in the same order when first added. What is the easiest way to loop through a dictionary containing strings as the key value and retrieving them in ascending order by key? For example, you had this: d = {'b' : 'this is b', 'a': 'this is a' , 'c' : 'this is c'}

Python dictionary from an object's fields

Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this: &gt;&gt;&gt; class Foo: ... bar = 'hello' ... baz = 'world' ... &gt;&gt;&gt; f = Foo() &gt;&gt;&gt; props(f) { 'bar' : 'hello', 'baz' : 'world' } NOTE: It should not include methods. Only fields.

python - How do you retrieve items from a dictionary in the order that they're inserted?

Is it possible to retrieve items from a Python dictionary in the order that they were inserted?

python - How can I make a dictionary from separate lists of keys and values?

I want to combine these: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] Into a single dictionary: {'name': 'Monty', 'age': 42, 'food': 'spam'}

python - Dictionary or If statements, Jython

I am writing a script at the moment that will grab certain information from HTML using dom4j. Since Python/Jython does not have a native switch statement I decided to use a whole bunch of if statements that call the appropriate method, like below: if type == 'extractTitle': extractTitle(dom) if type == 'extractMetaTags': extractMetaTags(dom)

Is a Python dictionary an example of a hash table?

One of the basic data structures in Python is the dictionary, which allows one to record "keys" for looking up "values" of any type. Is this implemented internally as a hash table? If not, what is it?

python - Is there a "one-liner" way to get a list of keys from a dictionary in sorted order?

The list sort() method is a modifier function that returns None. So if I want to iterate through all of the keys in a dictionary I cannot do: for k in somedictionary.keys().sort(): dosomething() Instead, I must: keys = somedictionary.keys() keys.sort() for k in keys: dosomething() Is there a pretty way to iterate t...

python - Interface to versioned dictionary

I have an versioned document store which I want to access through an dict like interface. Common usage is to access the latest revision (get, set, del), but one should be able to access specific revisions too (keys are always str/unicode or int). from UserDict import DictMixin class VDict(DictMixin): def __getitem__(self, key): if isinstance(key, tuple): docid, rev = key e...

python - List all words in a dictionary that start with <user input>

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string? Ex: User: "abd" Program:abdicate, abdomen, abduct... Thanks! Edit: I'm using python, but I assume that this is a fairly language-independent problem.

python - Check if a given key already exists in a dictionary and increment it

How do I find out if a key in a dictionary has already been set to a non-None value? I want to increment the value if there's already one there, or set it to 1 otherwise: my_dict = {} if my_dict[key] is not None: my_dict[key] = 1 else: my_dict[key] += 1

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python