Updated October 21, 2023
Introduction to Python Collections
Python relies heavily on collections, which are essential data structures that facilitate the organization and manipulation of data. They play a crucial role in data storage and management, enabling programmers to store, access, and manipulate data efficiently. Python offers diverse built-in collection types, each tailored to specific use cases. Lists provide ordered, mutable sequences, while tuples offer immutable collections. Sets store unique, unordered elements, and dictionaries allow key-value pair mappings. The collections module extends these capabilities, offering specialized data structures like namedtuples and deques. The significance of collections lies in their versatility, simplifying data handling and empowering developers to tackle various programming tasks effectively.
Table of Contents
Python Collections: Data types
Collections are data types that are shipped into Python under the collection list. It holds a large number of containers which are largely useful. These collections are object-driven since they are pulled from a separate list called collections. Thus, for accessing these data types, object declarations are expected.
The key collection of Python is listed below:
1. OrderedDict
OrderDict is very similar to normal Dict, except it is more efficient for reordering operations. The ordered dictionary maintains its sequence of insertion very strictly. Some of the protocols of the ordered dictionary are below,
- When a key that is the same as the existing key is inserted, the ordered dictionary collection replaces the existing key with the new key.
- Deleting an entry and reinserting it inserts the new entry as the last item.
- The usual dict was intended to be extremely first-class at mapping operations.
- Algorithmically, OrderedDict is able to grip the recurrent rearrangement process well again than dict.
The key functions used in a dictionary are as below
Functions | Description |
Python Dictionary clear() | Removes all Items |
Python Dictionary copy() | Returns Shallow Copy of a Dictionary |
Python Dictionary fromkeys() | Creates a dictionary from a given sequence |
Python Dictionary get() | Find the value of a key |
Python Dictionary items() | returns view of dictionary’s (key, value) pair |
Python Dictionary keys() | Prints the keys |
Python Dictionary popitem() | Remove the last element of the dictionary |
Python Dictionary setdefault() | Inserts a Value if Key is not Present |
Python Dictionary pop() | removes and returns elements having given key |
Python Dictionary values() | returns view of all values in the dictionary |
Python Dictionary update() | Updates the Dictionary |
Example:
from collections import OrderedDict
o=OrderedDict()
p=OrderedDict({'a':1,'b':2})
o['a']=3
o['b']=2
o['c']=1
o.popitem()
print('Print the keys :', o.keys())
print('Print the Values :', o.values())
print("Value of key a = ", o.get('a'))
print(p)
Output:
2. Counter
This is another container of the dict subclass. A “counter” specifically designed to keep track of the number of occurrences of each item in a data set. The counter acts like a dictionary in which one element is an iterable key, and the value is the number of times the key element is present in that dictionary. So in the collections module, the counter() function takes a key as an argument and returns a dictionary. Let’s see how it works:
Example:
from collections import Counter
a=(1,2,3,1)
b=[1,2,3]
c={1,2,3,1}
d={'1':'anand','2':'kumar','3':'ravi'}
e='anand'
print('Count of a : ',Counter(a))
print('Count of b : ',Counter(b))
print('Count of c : ',Counter(c)) #sets do not allow duplicates
print('Count of d : ',Counter(d))
print('Count of e : ',Counter(e)) #counter on string
print('print most common value in a :',Counter(a).most_common(1))
Output:
Points to ponder:
- Using a counter on the dictionary is considered a manual initiation of count values to the keys mentioned
- element() method is used for iteration on the counter
- most_common() is used for finding the value with the most number of frequencies
3. Deque
In Python collections, deque represents a queue that is double-ended and which allows values to be added to both the front and rear of the queue. Operations allowed in a double-ended queue are as below,
- append() – Append value to the right
- appendleft() – append value to the left
- pop() – delete value to the right end
- popleft() – delete value to the left end
Example:
import collections
a=collections.deque('anand')
b=collections.deque((1,2,2))
c=collections.deque({1,2,3,1})
print('Queue_a',a)
print('Queue_b',b)
print('Queue_c',c)
a.append('#')
a.appendleft('#')
print('After append :',a)
b.pop()
b.popleft()
print('After Removal :',b)
c.clear()
print('After clearing the Queue :',c)
Output:
4. NamedTuple
Named tuples are very closely related to the dictionary because dictionaries here to keys are tagged with values. The key difference between dictionaries and named tuples is these named tuples allow access to their elements as both key-value and iteration. key operations performed with named tuples are as below,
Here the attribute values can be accessed through indexes, whereas dictionaries do not allow the same.
Example:
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(11, y=22) # instantiate with positional or keyword arguments
print(p[0] + p[1]) # indexable like the plain tuple (11, 22)
Output :
5. Defaultdict
This feature operates similarly to a dictionary but does not verify the accuracy of the key used to access the data. If a wrong key is declared, it initializes the key element of the data type passed as an argument, utilizing a data type called default_factory. No errors are thrown in this case. It behaves like a dictionary but takes the first argument as a default data type.
Example:
from collections import defaultdict
n = defaultdict(int)
n['a'] = 1
n['b'] = 2
print(n['c'])
Output:
Explanation: The code creates an object named ‘n’ using the defaultdict() function. The function assigns three keys to the object – ‘a’ with a value of ‘1’, ‘b’ with a value of ‘2’, and ‘c’ without any assigned value. Therefore, the default value for the key ‘c’ is ‘0’.
6. ChainMap
The ChainMap class in Python’s collections module combines multiple mappings into a single view. It allows access to the keys and values in the merged dictionaries without creating a new one.
Example:
from collections import ChainMap
# Creating two dictionaries
dict1 = {'a': 15, 'b': 22}
dict2 = {'b': 33, 'c': 47}
# Creating a ChainMap to combine them
combined_dict = ChainMap(dict1, dict2)
# Accessing values
print(combined_dict['a'])
print(combined_dict['b'])
print(combined_dict['c'])
# Accessing keys
print(list(combined_dict.keys()))
Output:
7. UserDict
Python’s collections module provides a class called UserDict which allows you to create custom dictionaries with methods and behaviors that you define. It’s a useful feature when you want to enhance or modify the functionality of Python dictionaries without directly inheriting from the built-in dict class.
Example:
from collections import UserDict
# Create a custom dictionary class that enforces values to be integers
class IntegerDict(UserDict):
def __setitem__(self, key, value):
if not isinstance(value, int):
raise ValueError("Values must be integers")
super().__setitem__(key, value)
# Create an instance of the custom dictionary
int_dict = IntegerDict()
# Add key-value pairs with integer values
int_dict['apple'] = 7
int_dict['banana'] = 4
# Attempt to add a non-integer value
try:
int_dict['cherry'] = 'not an integer' # This will raise a ValueError
except ValueError as e:
print(e)
# Access values as you would with a regular dictionary
print(int_dict['apple'])
print(int_dict['banana'])
Output:
8. UserList
The UserList class, a part of Python’s collections module, creates list-like objects with custom behavior and additional features. It serves as a convenient base class for building your list-like classes.
Example:
from collections import UserList
class MyList(UserList):
def remove_duplicates(self):
"""Remove duplicate elements from the list."""
self.data = list(set(self.data))
# Creating an instance of the custom list
my_list = MyList([16, 22, 27, 32, 22, 41, 16])
print("Original List:", my_list)
my_list.remove_duplicates()
print("List with Duplicates Removed:", my_list)
Output:
9. UserString
The UserString class is part of Python’s collections module. This design aims to enhance convenience in string manipulation by enabling the creation of a string-like object that offers greater mutability and user-friendliness.
Example:
from collections import UserString
# Create a UserString object
user_string = UserString("Hello, World!")
# You can access and modify the contents of the UserString
print(user_string)
# Change the string
user_string.data = "Hello, Welcome to EDUCBA!"
# You can now modify the string
print(user_string)
# You can use various string operations
user_string.data = user_string.data.upper()
print(user_string)
Output:
Conclusion
Python’s collections module offers a valuable array of data structures that extend the capabilities of the built-in types. Whether counting elements, handling defaults, maintaining order, or creating user-defined containers, these tools enhance code efficiency and readability. The module equips Python programmers with versatile data management solutions.
Recommended Articles
We hope that this EDUCBA information on “Python Collections” was beneficial to you. You can view EDUCBA’s recommended articles for more information.