Python offers a rich set of built-in and extended data structures to efficiently manage and process data. In this blog, we’ll deep dive into essential ones: List, Tuple, Dictionary (Dict), Set, Frozenset, and also explore some powerful structures from the collections
and dataclasses
modules. We’ll cover their properties, use-cases, constructors, and how to convert between them using intuitive examples.
Note: Since Python 3.7, dictionaries preserve insertion order as a language feature (it was an implementation detail in 3.6).
Table of Contents
List
Description
A list is an ordered, mutable collection of elements. Lists are ideal when you need to work with sequences that might change over time.
Common Syntax
list([iterable])
Features
-
Mutable
-
Ordered
-
Indexed
-
Allows duplicates
Constructor
my_list = list([1, 2, 3])
my_list = [1, 2, 3] # More common
Conversions
list('nikhil') # From string
list(range(5)) # From range
list(('a', 'e', 'i')) # From tuple
list({'a': 1, 'b': 2}) # From dict (keys only)
list({'a', 'b', 'c'}) # From set
list(frozenset('abc')) # From frozenset
Use Cases
-
Managing ordered sequences
-
Stacks, queues, dynamic collections
Tuple
Description
A tuple is similar to a list but is immutable. Useful when you want to represent fixed sets of items.
Common Syntax
tuple([iterable])
Features
-
Immutable
-
Ordered
-
Indexed
-
Hashable
Constructor
my_tuple = tuple([1, 2, 3])
my_tuple = (1, 2, 3)
Conversions
tuple('nikhil') # From string
tuple(range(5)) # From range
tuple(['a', 'e', 'i']) # From list
tuple({'a': 1, 'b': 2}) # From dict (keys only)
tuple({'a', 'b', 'c'}) # From set
tuple(frozenset('abc')) # From frozenset
Use Cases
-
Read-only data
-
Dictionary keys
-
Returning multiple values from functions
Dictionary (dict)
Description
A dictionary maps unique keys to values. It’s great for lookups and structured data.
Common Syntax
dict()
dict(mapping)
dict(iterable)
dict(**kwargs)
Features
-
Mutable
-
Ordered (since Python 3.7)
-
Keys must be hashable
Constructor
my_dict = dict(x=5, y=10)
dict([('x', 5), ('y', 10)])
dict({'x': 5, 'y': 10})
Conversions
dict(zip(['a', 'b'], [1, 2]))
Use Cases
-
Configuration storage
-
JSON-like structures
-
Fast lookup tables
Set
Description
A set is an unordered collection of unique items. Great for membership testing and deduplication.
Common Syntax
set([iterable])
Features
-
Mutable
-
Unordered
-
Unique elements
Constructor
my_set = set([1, 2, 3])
my_set = {1, 2, 3}
Conversions
set('nikhil') # From string
set(range(5)) # From range
set(['a', 'e', 'i']) # From list
set(('a', 'e')) # From tuple
set({'a': 1, 'b': 2}) # From dict (keys only)
set(frozenset('abc')) # From frozenset
Use Cases
-
Eliminating duplicates
-
Membership checks
-
Set algebra operations
Frozenset
Description
A frozenset is an immutable version of a set. It supports all set operations but can’t be modified after creation.
Common Syntax
frozenset([iterable])
Features
-
Immutable
-
Unordered
-
Unique elements
-
Hashable
Constructor
frozenset(['a', 'b', 'c'])
Conversions
frozenset('nikhil') # From string
frozenset(range(5)) # From range
frozenset(['a', 'e', 'i']) # From list
frozenset(('a', 'e', 'i')) # From tuple
frozenset({'a': 1, 'b': 2}) # From dict (keys only)
frozenset({'a', 'e', 'i'}) # From set
Use Cases
-
Dictionary keys
-
Thread-safe operations
-
Set constants
NamedTuple (from collections
)
Description
Like tuples, but with named fields accessible by dot notation. Improves readability of fixed-size records.
Syntax
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
Example
p = Point(10, 20)
print(p.x, p.y)
Use Cases
-
Lightweight data structures
-
Replacing tuples with readable attributes
Deque (from collections
)
Description
A deque (double-ended queue) supports fast append and pop operations from both ends.
Syntax
from collections import deque
d = deque([1, 2, 3])
Example
d.appendleft(0)
d.append(4)
d.pop()
d.popleft()
Use Cases
-
Queues and stacks
-
Efficient front/back operations
DataClass (from dataclasses
)
Description
A dataclass is a decorator that automatically adds special methods like __init__
, __repr__
, and __eq__
to user-defined classes.
Syntax
from dataclasses import dataclass
@dataclass
class Item:
name: str
price: float
Example
item = Item('Book', 12.99)
print(item.name)
Use Cases
-
Structured, mutable records
-
Cleaner and more readable object definitions
Summary
Feature | List | Tuple | Dict | Set | Frozenset | NamedTuple | Deque | DataClass |
---|---|---|---|---|---|---|---|---|
Mutable | Yes | No | Yes | Yes | No | No | Yes | Yes |
Hashable | No | Yes | No | No | Yes | Yes | No | Custom |
Ordered | Yes | Yes | Yes | No | No | Yes | Yes | Yes |
Indexed | Yes | Yes | No | No | No | Yes (named) | Yes | Yes |
Unique Elements | No | No | Keys | Yes | Yes | No | No | No |
Can Be Dict Key | No | Yes | – | No | Yes | Yes | No | Yes (if frozen) |
Best Practices
-
Use lists for ordered, modifiable sequences.
-
Use tuples or namedtuples for fixed-length, immutable sequences.
-
Use dicts for mappings and fast key-based access.
-
Use sets and frozensets for uniqueness and set operations.
-
Use deques for efficient front/back operations.
-
Use dataclasses when modeling structured, mutable data objects.
Mastering these structures will make your Python code more efficient and expressive. Save this guide as your go-to reference for Python collections!