python remove obect from array using condition
<p>I have a list of dictionaries like below:</p>

<pre><code>tt = [
{
     'property1': 'value1',
     'property2': 'value2',
     'property3': 'value3'
},
{
     'property1': 'value4',
     'property2': 'value5',
     'property3': 'value6'
},
..............................
.............................
]
</code></pre>

<p>How i can remove <code>'property3' : 'value6'</code> from above list of dictionaries?</p>

<p>To be precise, i need to remove all instances of key('property3') with specific value('value6') from all dictionaries in the list?</p>

<p>I am in need of fastest method, because list can be vary large.</p>

<p>Sorry, I didn't notice that the poster only wanted to remove specific properties. Here's the correct solution:</p>

<pre><code>tt=[{k:v for k,v in i.items() if k != 'property3' or v != 'value6'} for i in tt]
</code></pre>

<p>LazyScripter's solution will delete dictionary elements where the key OR the value OR both match.  If I understand the original question, I don't think this is what was desired.</p>

<p>I've also compared the efficiency of the two proposed solutions and the one-liner seems to be about 12X faster...</p>

<pre><code>python -m cProfile test.py
</code></pre>

<p>outputs:</p>

<pre><code>1780059 function calls (1500059 primitive calls) in 2.759 seconds

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    ...
        1    0.026    0.026    2.759    2.759 test.py:1(&lt;module&gt;)
        1    0.036    0.036    0.917    0.917 test.py:19(search)
        1    0.035    0.035    0.073    0.073 test.py:33(search2)
    ...
</code></pre>

<p>For the following code:</p>

<pre><code>import copy

tt = [
{
     'property1': 'value1',
     'property2': 'value2',
     'property3': 'value3'
},
{
     'property1': 'value4',
     'property2': 'value5',
     'property3': 'value6'
}
]

d=[]
[d.extend(copy.deepcopy(tt)) for _ in range(10000)]

def search(dict, key=None, val=None):
    c_dict = copy.deepcopy(dict)
    for i, n in enumerate(dict):
        for k, v in n.iteritems():
            if key and val and k == key and val == v:
                c_dict[i].pop(k)
#            elif key and k == key:                                                                                                                                                                
#                c_dict[i].pop(k)                                                                                                                                                                  
#            elif v and val == v:                                                                                                                                                                  
#                c_dict[i].pop(k)                                                                                                                                                                  
            else:
                continue
    return c_dict


def search2(d,a,b):
    return [{k:v for k,v in i.items() if k != a or v != b} for i in d]

search (d, 'property3', 'value6')

search2(d, 'property3', 'value6')
</code></pre>

python python-3.x