C#
PY

C# to Python

10 lessons

Progress0%
1Introduction2Type Systems3Functions4Collections5Object-Oriented Programming6LINQ to Comprehensions7Async Programming8Ecosystem9Decorators and Metaprogramming10Error Handling
All Mirror Courses
C#
PY
Collections
MirrorLesson 4 of 10
Lesson 4

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.

Mirror Card
C#
From C#:

In C#, you're familiar with collections.

PY
In Python:

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:

PY
Python 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#:

C#
C# (What you know)
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;
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

C# List<T> → Python list (square brackets, no type parameter)

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

C# Dictionary<K,V> → Python dict (curly braces, colon separator)

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

C# HashSet<T> → Python set (curly braces, no key-value pairs)

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

.Count (C#) → len() built-in function in Python

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

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.

C#
C#
List<int> nums = new() { 1, 2, 3 };
nums.Add(4);
nums.Remove(2);
nums.Contains(3);
int n = nums.Count;
PY
Python
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.

C#
C#
var d = new Dictionary<string, int>();
d["x"] = 1;
int val = d["x"];
bool has = d.ContainsKey("x");
d.Remove("x");
PY
Python
d = {}
d["x"] = 1
val = d["x"]
has = "x" in d
del d["x"]

# Safe access with default
val = d.get("missing", 0)  # like GetValueOrDefault
Common Pitfall
Accessing a missing key with d[key] raises KeyError. Use d.get(key, default) for safe access — equivalent to TryGetValue.

3. Tuple — Immutable Sequences

Python tuples are immutable sequences. They are used where C# would use a ValueTuple or a fixed-size read-only array.

C#
C#
(string, int) pair = ("Alice", 30);
var (name, age) = pair; // deconstruction
PY
Python
pair = ("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#
C#
// C# Range (C# 8+)
var sub = list[1..4]; // index 1 to 3
var last = list[^1];  // last element
PY
Python
nums = [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] — reversed
Rule of Thumb
Slicing syntax is [start:stop:step]. Omitting start defaults to 0; omitting stop defaults to end.

Common 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)
Common Pitfall
Don't assume Python works exactly like C#. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C# code in Python to practice these concepts.
PreviousNext