Introduction to Python Collections
In programming terms, data is usually not a single piece of information. More often than not, we deal with a bunch of related information. This could be a list of users, a list of unique tags, or a list that maps product IDs to their corresponding prices. In programming terms, this is where collections come in.
So, if you have ever wondered what is collection in Python, the simplest explanation is that it is a container that can hold multiple items in a single variable. Unlike regular data types such as an integer or a string, Python collections allow you to do this. Get hands-on exposure with Python collections in our software training institute in Chennai.
Types of Collections in Python
There are two types of collections in Python:
- Built-in Collection Data Types: These are lists, tuples, sets, and dictionaries.
- The Python Collections Module: This is where we find datatypes such as namedtuple, deque, and Counter.
1. Built-in Collection Data Types in Python
Before moving on to the specific module, every programmer should know about the four basic collection data types in Python.
Comparison Table: Built-in Collections Python
| Collection | Ordered? | Mutable? | Duplicates? | Use Case |
| List | Yes | Yes | Yes | General-purpose sequences |
| Tuple | Yes | No | Yes | Fixed data (Coordinates, Records) |
| Set | No | Yes | No | Unique items, Math operations |
| Dictionary | Yes (3.7+) | Yes | No (Keys) | Key-Value mappings (JSON-like) |
A. Lists (The Flexible Sequence)
The list is the most commonly used collection in Python. A list is an ordered collection of elements that can hold any type of object.
Real-time Example: A shopping cart in an online shopping site where items are added and removed.
shopping_cart = [“Apple”, “Banana”, “Milk”]
shopping_cart.append(“Bread”) # Adding
shopping_cart[1] = “Organic Banana” # Updating
print(shopping_cart)
B. Tuples (The Constant Record)
The tuple is similar to lists. However, unlike lists, tuples are immutable. This means that once a tuple is built, it cannot be changed.
Real-time Example: Latitude and Longitude values.
location = (40.7128, 74.0060)
# location[0] = 41.0 # This would raise a TypeError
C. Sets (The Unique Gatherer)
The set is unordered and does not allow any repeating elements. This is the best collection type to check if an object is within a set.
Real-time Example: A list of IP addresses for a website.
visited_ips = {“192.168.1.1”, “10.0.0.5”}
visited_ips.add(“192.168.1.1”) # No change, duplicates ignored
print(visited_ips)
D. Dictionaries (The Key-Value Map)
The dictionary is a collection of elements stored in a key-value format. This is the best collection type for quick lookups.
Real-time Example: A dictionary where the key is “username,” and the value is “jdoe123”.
user = {
“id”: 1,
“name”: “John”,
“email”: “[email protected]”
}
print(user[“name”])
Learn from the basics with our Python tutorial for beginners.
2. The Python Collections Module
While the built-in types are good, at times you want more “horsepower.” The Python collections module gives you access to these specialized types, which can make your code faster and more readable.
A. namedtuple
This assigns names to each element in a tuple. This makes your code much more readable while maintaining the benefits of immutability.
from collections import namedtuple
# Define the structure
Point = namedtuple(‘Point’, [‘x’, ‘y’])
p = Point(11, y=22)
print(p.x, p.y) # Access by name instead of index p[0]
B. deque (Double Ended Queue)
The Python collections module’s deque type is designed to add or remove elements at both ends. Normal lists are slow at “insert(0, x)” because all other elements have to be shifted.
Real-time Example: The “Undo” button in any editor or a process scheduler.
from collections import deque
queue = deque([“Task1”, “Task2”])
queue.append(“Task3”) # Right side
queue.appendleft(“Task0”) # Left side (very fast)
queue.popleft() # Removes “Task0”
C. Counter
This is a dictionary subclass for counting hashable objects. It’s the best way to get the frequency of objects in a collection in Python.
Real-time Example: Finding the frequency of words in a document to determine keywords.
from collections import Counter
text = “apple banana apple cherry banana apple”
counts = Counter(text.split())
print(counts) # Output: Counter({‘apple’: 3, ‘banana’: 2, ‘cherry’: 1})
print(counts.most_common(1)) # Find the top item
D. defaultdict
If you try to access a key in a normal dictionary that doesn’t exist, you’ll get a KeyError. This type gives you a default value for nonexistent keys.
from collections import defaultdict
# Default value is an empty list
fruit_groups = defaultdict(list)
fruit_groups[‘citrus’].append(‘Orange’)
print(fruit_groups[‘citrus’]) # [‘Orange’]
print(fruit_groups[‘berries’]) # Returns [] instead of an error
E. OrderedDict
Until Python 3.7, normal dictionaries do not remember the insertion order. While normal dictionaries now do, they are still good for specific reordering operations like moving an element to the end. Our Python interview questions and answers will help you ace your technical rounds.
3. Real-World Application: Log Analysis
Now, let’s analyze how the collections in the Python language are used in a real program to analyze the server logs.
Problem Statement: We have a set of IP addresses that have accessed our server, and we would like to determine the top 3 most frequent visitors.
from collections import Counter, deque
# A list of IP logs (simulated)
logs = [“192.168.1.1”, “10.0.0.5”, “192.168.1.1”, “172.16.0.1”, “192.168.1.1”, “10.0.0.5”]
# 1. Count frequencies using Counter
ip_counts = Counter(logs)
# 2. Get the top 3 IPs
top_ips = ip_counts.most_common(3)
print(“Top Visitors:”)
for ip, count in top_ips:
print(f”IP: {ip} | Requests: {count}”)
# 3. Use a deque to maintain a “Recent Activity” window of size 5
recent_activity = deque(maxlen=5)
for log in logs:
recent_activity.append(log)
print(f”Last 5 activities: {list(recent_activity)}”)
Explore our Python project ideas to learn deeper.
When to Use Which Collection?
Every Python Collection is used for the following purposes:
| Collection | Purpose |
| dict | Fast lookups by key |
| set | Unique elements only |
| collections.deque | Fast appends/pops at both ends |
| collections.Counter | Counting occurrences |
| collections.namedtuple | Immutable records with names |
| list | Ordered list of items |
Conclusion
The ability to master the collections in the Python language is the hallmark of a proficient programmer. Being proficient in the collections in the Python language means that you are not just proficient in the language, but also that your code is not just optimized but also readable.
Whether you are working with a simple list or a counter to analyze the trends in the data, the ability to understand what constitutes a collection in the Python language ensures that you are always working with the right tool for the job. Enroll in our Python training in Chennai for a promising career in software development.
