Skip to main content

Hey everyone! Today we are going to dive into two powerful utilities in Python's collections module – defaultdict and Counter. These data structures can significantly simplify certain tasks, especially when working with dictionaries and counting elements. Let's explore how they work and their advantages.

defaultdict in Python

  • defaultdict is a subclass of Python's built-in dict class.

  • It overrides the default behavior of the dict class by providing a default value for a nonexistent key.

This is particularly useful when you want to avoid KeyError exceptions.

1. Using defaultdict

To use defaultdict, you must import it from the collections module:

from collections import defaultdict                 # Importing defaultdict from collections module  

# Example: Counting occurrences of letters in a word
word = 'programming' # Define a word
letter_count = defaultdict(int) # Create defaultdict with default value as int (0)

for letter in word: # Iterate over each letter in the word
letter_count[letter] += 1 # Increment the count of each letter

print(letter_count) # Output: defaultdict(<class 'int'>, {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1})

2. Advantages of defaultdict

Here are some of the key advantages of using defaultdict:

  • Avoids KeyError: You don't have to check whether a key exists before using it.
  • Simplifies code: Reduces the need for conditional logic to handle missing keys.
  • Customizable defaults: You can define any default factory function (like int, list, str, etc.).

Counter in Python

  • Counter is a subclass of dict designed to count hashable objects like elements in a list or characters in a string.

  • It makes it easy to tally occurrences without writing manual loops.

1. Using Counter

To use Counter, you must also import it from the collections module:

from collections import Counter                      # Importing Counter from collections module  

# Example: Counting the frequency of characters in a word
word = 'banana' # Define a word
char_count = Counter(word) # Create Counter to count characters in the word

print(char_count) # Output: Counter({'a': 3, 'n': 2, 'b': 1})

2. Advantages of Counter

Here are some of the key advantages of using Counter:

  • Efficient counting: Automatically counts elements without explicit loops.
  • Built-in operations: Supports common tasks like finding the most common elements (most_common()).
  • Works with all iterables: Can be used with strings, lists, tuples, and more.

3. Common Counter Methods

Here are a few useful methods provided by the Counter class:

# Example: Using the most_common() method  
from collections import Counter # Importing Counter

word = 'mississippi' # Define a word
char_count = Counter(word) # Create Counter for character count

# Get the two most common characters
print(char_count.most_common(2)) # Output: [('i', 4), ('s', 4)]

# Example: Subtracting counts using subtract()
other_word = 'issippi' # Define another word
other_count = Counter(other_word) # Create Counter for second word

char_count.subtract(other_count) # Subtract counts from the second Counter
print(char_count) # Output: Counter({'m': 1, 'i': 1, 's': 0, 'p': 0})