Python Defaultdict – Int and List as a Default Factory

Free Python courses with 57 real-time projects - Learn Python

In today’s lesson, we will look at Python defaultdict, a subclass of the built-in dict class. So let’s start with defaultdict Python 3 tutorial.

Here, we will discuss what is Python Defaultdict with its syntax and exmaple. Moreover, we will study Python Defaultdict using Int and List as a defaultdict in Python.

Dictionaries are an important part of Python. They are containers to hold key-value pairs.

>>> issubclass(defaultdict,dict)   #You’ll need to import defaultdict from collections for this

Output

True

So, let’s start Python Defualtdict Tutorial.

What is Python Defaultdict

Python Defaultdict – Int and List as a Default Factory

What are Python Dictionaries?

Before we proceed to defaultdict in Python, we suggest you read up on Python Dictionaries.

A python dictionary is a container that holds key-value pairs. Keys must be unique, immutable objects. This means that a Python tuple can be a key, but a python list can’t since it is mutable.

>>> {[1,2]:1,2:2}
Traceback (most recent call last):
File "<pyshell#355>", line 1, in <module>
{[1,2]:1,2:2}

Output

TypeError: unhashable type: ‘list’

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

As you can see, a list, then, is unhashable. Let’s take a quick example to see how to define a python dictionary.

>>> mydict={'a':1,'b':2,'c':3}
>>> mydict['b']

Output

2

We can also use the dict() function.

>>> otherdict=dict((['a',1],['b',2],['c',3]))
>>> otherdict

Output

{‘a’: 1, ‘b’: 2, ‘c’: 3}

To get a list of keys, we use the keys() method.

>>> otherdict.keys()

Output

dict_keys([‘a’, ‘b’, ‘c’])

But we have one problem. If the key does not exist, and we try to access it, it raises a KeyError.

>>> otherdict['d']
Traceback (most recent call last):
File "<pyshell#364>", line 1, in <module>
otherdict['d']

Output

KeyError: ‘d’

What is Python Defaultdict?

To deal with this situation, we have defaultdict in Python. First, we must import it from Python collections module.

>>> from collections import defaultdict

Here, we use the Python defaultdict() factory function. It takes, as an argument, a function object that will return a value.

Actually, if we don’t provide a value for a particular key, it will take that value for it.

Let’s take a look at the syntax.

Python Defaultdict – Syntax

Before Python defaultdict syntax, we revise python syntax.

As we discussed, we first import defaultdict from the ‘collections’ module. Let us see this Python defaultdict example.

>>> from collections import defaultdict

Next, we define a python function to return a default value for the keys we don’t initialize.

>>> def defaultvalue():
                return 0

Now, we create a Python defaultdict using the defaultdict() function. To this, we pass the function defaultvalue as a function argument.

Remember, you must pass the function object, you don’t have to call it with parentheses.

>>> otherdict=defaultdict(defaultvalue)

Let’s now define values for keys ‘a’, ‘b’, and ‘c’.

>>> otherdict['a']=1
>>> otherdict['b']=2
>>> otherdict['c']=3

What if we access the value for key ‘d’? We haven’t initialized it yet.

>>> otherdict['d']

As you can see, it assigned it the default value we specified through function defaultvalue().

Let’s see the whole code at once now.

>>> def defaultvalue():
                return 0
>>> otherdict=defaultdict(defaultvalue)
>>> otherdict['a']=1
>>> otherdict['b']=2
>>> otherdict['c']=3
>>> otherdict['d']

Another Example of Python Defaultdict

Let’s take another python defaultdict example. We take a defaultdict in python to hold the marks of students. Also, we let the default value for marks be 35.

>>> marks=defaultdict(lambda :35)
>>> marks['Ayushi']=95
>>> marks['Gigi']=86
>>> marks['Ayushi']

Output

95

Here, we used a Lambda Expression since all we need to do in it is to return a value. So why waste space, right?Now, if we try to access the value to a key that doesn’t exist, it saves 35 to it.

>>> marks['Oshin']

Output

35

Finally, these are the keys to this python defaultdict now:

>>> marks.keys()

Output

dict_keys([‘Ayushi’, ‘Gigi’, ‘Oshin’])

More on Defaultdict in Python

Let’s see what happens if we return a None from the function.

>>> a=defaultdict(lambda :None)
>>> a[1]
>>> type(a[1])

Output

<class ‘NoneType’>

What if a python exception was raised in the factory function?

>>> def defaultvalue():
                print(1/0)
                return 0
>>> a=defaultdict(defaultvalue)
>>> a[1]

Output

Traceback (most recent call last):File “<pyshell#386>”, line 1, in <module>

a[1]

File “<pyshell#384>”, line 2, in defaultvalue

print(1/0)

ZeroDivisionError: division by zero

Python defaultdict supports all operations that a regular dictionary does. These include keys() and len().

>>> dir(a)

Output

[‘__class__’, ‘__contains__’, ‘__copy__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__missing__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__setitem__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘clear’, ‘copy’, ‘default_factory’, ‘fromkeys’, ‘get’, ‘items’, ‘keys’, ‘pop’, ‘popitem’, ‘setdefault’, ‘update’, ‘values’]

Let’s know about Exception handling in python.

1. __missing__(key)

This Python method is called by the __getitem__() method of the dict class when the requested key isn’t found.

>>> from collections import defaultdict
>>> a=defaultdict(lambda :0)
>>> a['a']=1
>>> a['b']=2
>>> a['c']=3
>>> a.__missing__('d')

It returned 0 here because that’s what we specified as a default value through a lambda expression to the defaultdict() factory function.

>>> a.__getitem__('a')

Output

1

>>> a.__getitem__('e')

Using ‘list’ as a Default Factory

If we pass ‘list’ (without the quotes) to the defaultdict() Python function, we can group a sequence of key-value pairs into a dictionary of lists.

We can do this to more types as well. We’ll see another in the next section.

>>> a=[('a',1),('b',2),('c',3)]
>>> b=defaultdict(list)
>>> for i,j in a:
                b[i].append(j)              
>>> b

Output

defaultdict(<class ‘list’>, {‘a’: [1], ‘b’: [2], ‘c’: [3]})

Let’s explain what this code does.

First, we define ‘a’ as a list of tuples to hold the key-value pairs. Next, we pass ‘list’ to defaultdict(), and store this in ‘b’. This tells the interpreter that b will hold a dictionary with values that are list.

Then, we traverse on the tuples using names ‘I’ and ‘j’ with a for-loop. In this, we append j to b[i].

What this means is that we take the second value in each tuple in ‘a’, and append it to the python defaultdict for each key (first value in each tuple).

For ‘c’ in the list ‘a’, as a key in the defaultdict ‘b’, we append the value 3 to the python defaultdict to that key. This is its value now.

Using ‘int’ as a Default Factory

We can tell the interpreter to create a Python dictionary of integers. Here’s how.

>>> name='Bubbles'
>>> mydict=defaultdict(int)
>>> for i in name:
                mydict[i]+=1              
>>> mydict

Output

defaultdict(<class ‘int’>, {‘B’: 1, ‘u’: 1, ‘b’: 2, ‘l’: 1, ‘e’: 1, ‘s’: 1})

This way, we can use it as a counter. Of course, we could have done this with a regular dictionary as well. But here, we tell the interpreter that we’re going to work with only integer values.

So, this was all about Python Defaultdict Tutorial. Hope you like our explanation.

Python Interview Questions on Defaultdict

  1. What is Defaultdict in Python?
  2. What is the difference between Defaultdict and dict?
  3. How does Defaultdict work in Python?
  4. Explain the use of Defaultdict in Python.
  5. Is Defaultdict slower than dict?

Conclusion

With a subtle difference to a regular dictionary, we see that a python defaultdict is actually quite useful. It has a simple syntax as well and lets us define a default value for uninitialized keys.

It also lets us tell the interpreter that we want to work with a particular type of values.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

2 Responses

  1. Krunal Shah says:

    The below code runs into error in Python3. Works fine in Python2. Please suggest

    Code
    =======================================
    from collections import defaultdict

    marks = [(‘Shubham’, 89), (‘Pankaj’, 92), (‘JournalDev’, 99),(‘JournalDev’, 98)]

    dict_marks = defaultdict(list)

    for key, value in marks:
    dict_marks[key].append(value)

    print((dict_marks.items()))
    =======================================

    Error
    =======================================
    dict_marks[key].append(value)

    KeyError: ‘Shubham’
    =======================================

    • DataFlair Team says:

      Hello Krunal,

      Thanks for asking query on Python Defaultdict. We ran this code with Python 3.7.0 and it returned the following output:

      >>> print((dict_marks.items()))
      dict_items([(‘Shubham’, [89]), (‘Pankaj’, [92]), (‘JournalDev’, [99, 98])])
      Keep visting DataFlair

Leave a Reply

Your email address will not be published. Required fields are marked *