Collections
Collections
Introduction
In this lesson, you'll learn about collections in Python. Coming from C#, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C#, you're familiar with collections.
Python has its own approach to collections, which we'll explore step by step.
The Python Way
Let's see how Python handles this concept. Here's a typical example:
nums = [1, 2, 3]
scores = {"Alice": 90, "Bob": 85}
tags = {"c#", "dotnet"}
nums.append(4)
first = nums[0]
count = len(nums)Comparing to C#
Here's how you might have written similar code in C#:
List<int> nums = new() { 1, 2, 3 };
Dictionary<string, int> scores = new() {
["Alice"] = 90, ["Bob"] = 85
};
HashSet<string> tags = new() { "c#", "dotnet" };
nums.Add(4);
int first = nums[0];
int count = nums.Count;You may be used to different syntax or behavior.
C# List<T> → Python list (square brackets, no type parameter)
You may be used to different syntax or behavior.
C# Dictionary<K,V> → Python dict (curly braces, colon separator)
You may be used to different syntax or behavior.
C# HashSet<T> → Python set (curly braces, no key-value pairs)
You may be used to different syntax or behavior.
.Count (C#) → len() built-in function in Python
You may be used to different syntax or behavior.
Python lists are dynamic and untyped — they can hold mixed types
Step-by-Step Breakdown
1. List Basics
Python lists are dynamic arrays like C# List<T>, but without a type parameter — they can hold anything.
List<int> nums = new() { 1, 2, 3 };
nums.Add(4);
nums.Remove(2);
nums.Contains(3);
int n = nums.Count;nums = [1, 2, 3]
nums.append(4)
nums.remove(2)
3 in nums # True
n = len(nums)
# List also supports:
nums.pop() # remove & return last
nums.insert(0, 0) # insert at index
nums.sort()2. Dict Basics
Python dict is equivalent to C# Dictionary<K,V>. Key access, missing key behavior, and iteration all differ slightly.
var d = new Dictionary<string, int>();
d["x"] = 1;
int val = d["x"];
bool has = d.ContainsKey("x");
d.Remove("x");d = {}
d["x"] = 1
val = d["x"]
has = "x" in d
del d["x"]
# Safe access with default
val = d.get("missing", 0) # like GetValueOrDefault3. Tuple — Immutable Sequences
Python tuples are immutable sequences. They are used where C# would use a ValueTuple or a fixed-size read-only array.
(string, int) pair = ("Alice", 30);
var (name, age) = pair; // deconstructionpair = ("Alice", 30)
name, age = pair # unpacking (same as deconstruction)
# Single-element tuple needs trailing comma
single = (42,)
# Tuples are hashable — usable as dict keys
d = {("x", 1): "value"}4. Slicing
Python sequences support slicing — a concise way to extract sub-sequences. C# has no direct equivalent without LINQ or Range.
// C# Range (C# 8+)
var sub = list[1..4]; // index 1 to 3
var last = list[^1]; // last elementnums = [0, 1, 2, 3, 4, 5]
nums[1:4] # [1, 2, 3] — same as C# 1..4
nums[-1] # 5 — last element
nums[:3] # [0, 1, 2] — first 3
nums[::2] # [0, 2, 4] — every 2nd element
nums[::-1] # [5, 4, 3, 2, 1, 0] — reversedCommon Mistakes
When coming from C#, developers often make these mistakes:
- C# List<T> → Python list (square brackets, no type parameter)
- C# Dictionary<K,V> → Python dict (curly braces, colon separator)
- C# HashSet<T> → Python set (curly braces, no key-value pairs)
Key Takeaways
- List<T> → list [], Dictionary<K,V> → dict {}, HashSet<T> → set {}
- len() replaces .Count; 'in' operator replaces .Contains()
- Use dict.get(key, default) for safe access instead of indexer
- Python slicing [start:stop:step] provides concise sub-sequence extraction