Search

shirishweb

Your right place for "Right to Know"

Category

Programming

Python defaultdict() versus dict.get()

Python is a great language. Dict is one of data structure available in python which allows data to store in the form of key/value pair. Many times we might encounter a situation where we have to retrieve a value using the dictionary key. When our key is not found in the dictionary it will throw an exception of KeyError.

KeyError

The solution to this is using defaultdict or dict.get() to return a default value if the key is not found in the dictionary.

Lets dig into both of these

defaultdict can be found in collections module of python. So, in order to use it, we have to import it first as:

from collections import defaultdict
mydict = defaultdict(int)

defaultdict constructor takes default_factory as argument which is a callable. This can be

int : default will be integer value of 0

str : default will be empty string ”

list : default will be empty list []

and so on.

If we want our own default value then we can pass function pointer.

Lets say that we want default value to string ‘default’. We can achieve this in defaultdict as

def mydefault():
        return 'default'

mydict = defaultdict(mydefault)
print mydict['test']

will output ‘default

The same result can also be achieved using the dict.get method as

mydict = {}
mydict.get('test','default')

will output ‘default

Thus same result can be achieved using both approach. dict.get have to provide a default value every time it is called whereas using defaultdict we cant setup a default value only one time.

Now lets check the efficiency of both of them in term of execution time.

For this we will be using ipython notebook since it has got %timeit command to measure the execution of any python statements over any desired looping of same statements.

In Ipython notebook environment, let us create two function that implements defaultdict in one and dict.get method in another.

defining_two_defs

Here we have implemented a() function to execute defaultdict value retrieving and b() to use dict.get method. Now lets calculate the execution time of both function in 100 loops as

defaultdict_timeit

dict.get_timeit_2

First executing a() function over 100 loops we can see that it took over 8.88 micro sec per loop using the best of three.

dict.get_timeit

Here when we run b() function over 100 loops we can see that it tooks 19.3 micro sec per loop using the best of 3 approach.

Hence defaultdict seems more efficient over dict.get method.

Lets rerun this test again but this time will be using default %timeit command of notebook which will be looping the statement for default 1000 loops

defaultdict_timeit_2

Clearly here also defaultdict seems more efficient that dict.get method and the experiment shows that defaultdict more that two times faster than dict.get method.

Advertisements

Javascript Module: Basic Desing and Pattern

1. Creating a Module
First we need to understand about “Immediately Invoked Function Expression (IIFE)”. These IIFEs are simple function when declared, are called immediately.

IIFE

This will also create a new scope where we can put all out logics. This is an anonymous module. We can namespace this module as

Simple Module

2. Creating a private method

Private method simply is a method that are wrapped inside a module, intended to prevent access from externally outside of the module. This will make our methods more secure. Private methods are useful when we have to access sensitive data over the internet or make external server calls.

code3

3. Module Gateway: “Return”

Inside the module, we will reveal only part of the scope for external access. This is made possible by using the return statement within the Module. This return statement return object the mode and is made accessible from the module’s namespace.

code4

We can access this public method as:

code5

4. Code management with “Locally scoped object literals”

We see in above code that we aren’t namespacing our return object. We can also namespace our returned object with the module scope that makes our code more managed and readable. We can also segregate our private methods from return within the namespaced object.

code6

5. Desing better with revealing module pattern

With revealing module pattern we only return things that are necessary for external access for our module to work perfectly as intended. This create a public gateway to our module and get access to only this that we want to reveal.

code7

6. Extending our module

There might be some cases that our built method doesn’t work fully and lack some of the functionality needed on some cases specifically. In that case we can easily extend our pre built module and add some other functionality within it.

code8

Here we have extended our Module to NewModule that have new “newMethod” added methods.

NewModule has an argument Module || {}. Here we have passed pre-built Module to the NewModule. If this Module is not defined or “undefined” then we pass new object “{}” as argument to our NewModule and extend it.

Create a free website or blog at WordPress.com.

Up ↑