Python Collections
Python provides several built-in collection data types that help you store and organize data efficiently.The four main types are:
|
Collection
Type |
Ordered |
Mutable |
Allows
Duplicates |
|
List |
✅ Yes |
✅ Yes |
✅ Yes |
|
Tuple |
✅ Yes |
❌ No |
✅ Yes |
|
Set |
❌ No |
✅ Yes |
❌ No |
|
Dictionary |
✅ Yes (Python 3.7+) |
✅ Yes |
❌ Keys must be unique |
List
A list is an ordered, mutable (changeable) collection that can store items of different data types.
Tuple
A tuple is an ordered, immutable collection. Once created, its elements cannot be changed. Use tuples when you need a fixed collection of items.
Set
A set is an unordered collection of unique items. Duplicates are automatically removed. Use sets when you need to store unique elements and perform operations like union or intersection.
Dictionary (dict)
A dictionary stores data as key-value pairs. Keys must be unique, and each key maps to a value.
No comments:
Post a Comment