user1050619
user1050619

Reputation: 20906

deleting element from python dictionary

I need the most efficient way to delete few items from the dictionary, RIght now, Im using for stmt as given below..Thinking the same thing should be accomplished in few lines.

for eachitem in dicta:
                del eachitem['NAME']
                del eachitem['STATE']
                del eachitem['COUNTRY']
                del eachitem['REGION']
                del eachitem['LNAME']

dicta = [{'name','Bob','STATE':'VA','COUNTRY':'US','REGION':'MIDWEST','LNAME':'Brian',Salary:6000}]

I want only the salary item in the dictionary once its deleted. Any inputs are appreciated.

Upvotes: 2

Views: 342

Answers (3)

David Marx
David Marx

Reputation: 8558

EDIT: I had said dict comprehension was fastest: I was wrong. I had accidentally run %timeit test3 instead of %timeit test3(). See results below.

def test1():
    dicta = [{'NAME':'Bob','STATE':'VA','COUNTRY':'US','REGION':'MIDWEST','LNAME':'Brian','Salary':6000}]
    remove = ['NAME','STATE','COUNTRY','REGION','LNAME']
    for d in dicta:
        for r in remove:
            d.pop(r)

def test2():
    dicta = [{'NAME':'Bob','STATE':'VA','COUNTRY':'US','REGION':'MIDWEST','LNAME':'Brian','Salary':6000}]
    remove = ['NAME','STATE','COUNTRY','REGION','LNAME']
    for d in dicta:
        for r in remove:
            del d[r]

def test3():
    dicta = [{'NAME':'Bob','STATE':'VA','COUNTRY':'US','REGION':'MIDWEST','LNAME':'Brian','Salary':6000}]
    remove = ['NAME','STATE','COUNTRY','REGION','LNAME']    
    dicta = [{k:v for k,v in d.iteritems() if k not in remove } for d in dicta]

# this is really what OP was looking for, the other 3 tests are more generalized.    
def test4():    
    dicta = [{'NAME':'Bob','STATE':'VA','COUNTRY':'US','REGION':'MIDWEST','LNAME':'Brian','Salary':6000}]
    remove = ['NAME','STATE','COUNTRY','REGION','LNAME']    
    dicta = [e['Salary'] for e in dicta]

%timeit test1()
# 100000 loops, best of 3: 2.32 us per loop    
%timeit test2()
# 1000000 loops, best of 3: 1.68 us per loop    
%timeit test3()
# 100000 loops, best of 3: 3.23 us per loop
%timeit test4()
# 1000000 loops, best of 3: 1.46 us per loop

Upvotes: 1

mgilson
mgilson

Reputation: 310177

I suppose you could use:

for eachitem in dicta:
    for k in ['NAME','STATE','COUNTRY','REGION','LNAME']:
        del eachitem[k]

Or, if you only want 1 key:

for eachitem in dicta:
    salary = eachitem['SALARY']
    eachitem.clear()
    eachitem['SALARY'] = salary

This does everything in place which I assume you want -- Otherwise, you can do it out of place simply by:

eachitem = {'SALARY':eachitem['SALARY']}

Upvotes: 5

Abhijit
Abhijit

Reputation: 63777

If your example data is what you are dealing with, instead of deleting the elements, just recreate your dict with that lone key

dicta = [{'Salary':e['Salary']} for e in dicta]

or to me, it makes more sense, to just create a list instead of list of dicts

dicta = [e['Salary'] for e in dicta]

but depends on what you are planning to achieve

Upvotes: 6

Related Questions