Python Forum
deleting item from dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deleting item from dictionary
#1
i have item in a dictionary that needs to be deleted. which way is more pythonic?
    del mydict['deleteme']
    mydict.pop('deleteme')
i like the 2nd form because i can use it with a 2nd argument in cases where the item to be deleted may not actually be in the dictionary.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
pop returns the item being 'deleted'
del does not
so proper answer is 'It depends on context'
Reply
#3
so if i don't need the item i should use del instead of doing .pop without an assignment?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
That's what I'd do, but that doesn't necessarily mean it's the best way. Seems to me returning a value would take some extra clock cycles .
Reply
#5
yeah, even if the return value is ignore, it would be at least making it available. there is a "__delitem__" method for a dictionary. that might be intended for del.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
.pop is slightly slower than del (Python 3.6.3, GCC 4.8.5 linux):

Quote:python -m timeit "d = {'s':1 }; d.pop('s')"
Output:
1000000 loops, best of 3: 0.236 usec per loop
Quote:python -m timeit "d = {'s':1 }; del d['s']"
Output:
10000000 loops, best of 3: 0.14 usec per loop
Reply
#7
i need to be sure i don't replace instance of this where the returned value is used or a 2nd argument is given. in the latter, even when the return value is ignored, it ignores the case where the item is not actually present, avoiding raising an except, effectively operating as "be sure this item is not present".

results are similar in python3.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 5,544 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  How to modify item in dictionary? Winfried 7 3,385 Nov-21-2020, 07:12 PM
Last Post: bowlofred
  looking fo an expression that yields a dictionary without an item Skaperen 5 2,884 Apr-09-2019, 02:05 AM
Last Post: Skaperen
  curious syntax with dictionary item inselbuch 3 2,722 Mar-09-2019, 04:21 PM
Last Post: ichabod801
  Deleting the first item in linked list dan789 7 3,880 Mar-05-2019, 06:34 PM
Last Post: ichabod801
  Deleting value from dictionary. dbdb12 3 2,896 May-18-2018, 07:07 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020