python dict: get vs setdefault

前端 未结 8 1077
误落风尘
误落风尘 2020-12-04 15:04

The following two expressions seem equivalent to me. Which one is preferable?

data = [(\'a\', 1), (\'b\', 1), (\'b\', 2)]

d1 = {}
d2 = {}

for key, val in d         


        
相关标签:
8条回答
  • 2020-12-04 16:08

    There is no strict answer to this question. They both accomplish the same purpose. They can both be used to deal with missing values on keys. The only difference that I have found is that with setdefault(), the key that you invoke (if not previously in the dictionary) gets automatically inserted while it does not happen with get(). Here is an example: Setdefault()

    >>> myDict = {'A': 'GOD', 'B':'Is', 'C':'GOOD'} #(1)
    >>> myDict.setdefault('C')  #(2)
    'GOOD'
    >>> myDict.setdefault('C','GREAT')  #(3)
    'GOOD'
    >>> myDict.setdefault('D','AWESOME') #(4)
    'AWESOME'
    >>> myDict #(5)
    {'A': 'GOD', 'B': 'Is', 'C': 'GOOD', 'D': 'AWSOME'} 
    >>> myDict.setdefault('E')
    >>>
    

    Get()

    >>> myDict = {'a': 1, 'b': 2, 'c': 3}   #(1)
    >>> myDict.get('a',0)   #(2)
    1
    >>> myDict.get('d',0)   #(3)
    0
    >>> myDict #(4)
    {'a': 1, 'b': 2, 'c': 3}
    

    Here is my conclusion: there is no specific answer to which one is best specifically when it comes to default values imputation. The only difference is that setdefault() automatically adds any new key with a default value in the dictionary while get() does not. For more information, please go here !

    0 讨论(0)
  • 2020-12-04 16:10

    Your two examples do the same thing, but that doesn't mean get and setdefault do.

    The difference between the two is basically manually setting d[key] to point to the list every time, versus setdefault automatically setting d[key] to the list only when it's unset.

    Making the two methods as similar as possible, I ran

    from timeit import timeit
    
    print timeit("c = d.get(0, []); c.extend([1]); d[0] = c", "d = {1: []}", number = 1000000)
    print timeit("c = d.get(1, []); c.extend([1]); d[0] = c", "d = {1: []}", number = 1000000)
    print timeit("d.setdefault(0, []).extend([1])", "d = {1: []}", number = 1000000)
    print timeit("d.setdefault(1, []).extend([1])", "d = {1: []}", number = 1000000)
    

    and got

    0.794723378711
    0.811882272256
    0.724429205999
    0.722129751973
    

    So setdefault is around 10% faster than get for this purpose.

    The get method allows you to do less than you can with setdefault. You can use it to avoid getting a KeyError when the key doesn't exist (if that's something that's going to happen frequently) even if you don't want to set the key.

    See Use cases for the 'setdefault' dict method and dict.get() method returns a pointer for some more info about the two methods.

    The thread about setdefault concludes that most of the time, you want to use a defaultdict. The thread about get concludes that it is slow, and often you're better off (speed wise) doing a double lookup, using a defaultdict, or handling the error (depending on the size of the dictionary and your use case).

    0 讨论(0)
提交回复
热议问题