Core Data Structures in Python
Core Data Structures in Python
Section titled “Core Data Structures in Python”Python provides several built-in collection types that are used to store, organize, and manipulate data efficiently.
These data structures are flexible and map closely to collections in C# and JavaScript.
1. List
Section titled “1. List”nums = [1, 2, 3]What is a List?
Section titled “What is a List?”A list is:
- Ordered collection
- Mutable (can be changed)
- Can store duplicate values
- Can store mixed data types
Key Characteristics
Section titled “Key Characteristics”- Maintains insertion order
- Elements can be added, removed, or modified
- Index-based access
nums.append(4)nums[0] = 10C# Equivalent
Section titled “C# Equivalent”List<int> nums = new List<int> { 1, 2, 3 };JavaScript Equivalent
Section titled “JavaScript Equivalent”const nums = [1, 2, 3];2. Tuple
Section titled “2. Tuple”point = (4, 5)What is a Tuple?
Section titled “What is a Tuple?”A tuple is:
- Ordered collection
- Immutable (cannot be changed)
- Typically used for fixed collections of values
Key Characteristics
Section titled “Key Characteristics”- Faster than lists
- Often used for coordinates, pairs, and return values
- Safer when data should not change
x, y = point # tuple unpackingC# Equivalent
Section titled “C# Equivalent”(int x, int y) point = (4, 5);JavaScript Equivalent
Section titled “JavaScript Equivalent”JavaScript does not have tuples natively, but arrays are used:
const point = [4, 5];3. Dictionary (dict)
Section titled “3. Dictionary (dict)”user = {"name": "Ada"}What is a Dictionary?
Section titled “What is a Dictionary?”A dictionary:
- Stores key–value pairs
- Keys must be unique
- Values can be of any type
- Mutable
Key Characteristics
Section titled “Key Characteristics”- Fast lookups by key
- Insertion order preserved (Python 3.7+)
user["age"] = 30C# Equivalent
Section titled “C# Equivalent”Dictionary<string, string> user = new Dictionary<string, string>{ { "name", "Ada" }};JavaScript Equivalent
Section titled “JavaScript Equivalent”const user = { name: "Ada" };4. Set
Section titled “4. Set”unique = {1, 2, 2, 3}What is a Set?
Section titled “What is a Set?”A set is:
- Unordered collection
- Stores unique values only
- Automatically removes duplicates
Result:
{1, 2, 3}Key Characteristics
Section titled “Key Characteristics”- No indexing
- Extremely fast membership checks
- Useful for removing duplicates and set operations
2 in unique # TrueC# Equivalent
Section titled “C# Equivalent”HashSet<int> unique = new HashSet<int> { 1, 2, 2, 3 };JavaScript Equivalent
Section titled “JavaScript Equivalent”const unique = new Set([1, 2, 2, 3]);5. Summary Table
Section titled “5. Summary Table”| Python Type | Ordered | Mutable | Allows Duplicates | C# Equivalent | JavaScript Equivalent |
|---|---|---|---|---|---|
list | ✅ | ✅ | ✅ | List<T> | Array |
tuple | ✅ | ❌ | ✅ | (T1, T2) | Array |
dict | ✅ | ✅ | ❌ (keys) | Dictionary<TKey, TValue> | Object |
set | ❌ | ✅ | ❌ | HashSet<T> | Set |
6. When to Use What
Section titled “6. When to Use What”- List → Ordered, changeable collections
- Tuple → Fixed, read-only groupings
- Dictionary → Key-value relationships
- Set → Unique values and fast membership checks
7. Final Summary
Section titled “7. Final Summary”- Python’s core data structures are powerful and flexible
- They closely map to familiar C# and JavaScript collections
- Choosing the right structure improves performance and readability
- Understanding mutability and ordering is critical