C
PY

C to Python

10 lessons

Progress0%
1Variables & Types2Functions3Arrays → Lists4Structs → Classes & Dicts5Memory Management6String Handling7File I/O8Object-Oriented Programming9Exceptions and Context Managers10Standard Library and Tools
All Mirror Courses
C
PY
Arrays → Lists
MirrorLesson 3 of 10
Lesson 3

Arrays → Lists

Storing sequences of data

Introduction

In this lesson, you'll learn about arrays → lists 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 storing sequences of data.

PY
In Python:

Python has its own approach to storing sequences of data, 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
# Dynamic list — grows automatically
nums = [1, 2, 3, 4, 5]
n = len(nums)

# Append and extend
nums.append(6)
nums.extend([7, 8])

# Iteration
for num in nums:
    print(num)

# Slicing
sub = nums[1:4]   # [2, 3, 4]
rev = nums[::-1]  # reversed

# List comprehension
squares = [x**2 for x in range(10)]

# 2D list
matrix = [[1,2,3],[4,5,6],[7,8,9]]

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#include <stdio.h>
#include <string.h>

/* Fixed-size array */
int nums[5] = {1, 2, 3, 4, 5};
int n = sizeof(nums) / sizeof(nums[0]); /* length */

/* Iteration */
for (int i = 0; i < n; i++) {
    printf("%d\n", nums[i]);
}

/* 2D array */
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

/* No built-in dynamic array — use malloc */
int *dyn = malloc(10 * sizeof(int));
dyn[0] = 42;
free(dyn);
Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C arrays are fixed-size; Python lists are dynamic

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C arrays need sizeof tricks for length; Python uses len()

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

No malloc/free in Python — garbage collector handles memory

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

Python lists support slicing, comprehensions, and mixed types

Step-by-Step Breakdown

1. Dynamic Size

C arrays are fixed at compile time. Python lists grow dynamically with append() — no malloc/free needed.

C
C
int nums[5] = {1, 2, 3, 4, 5};
PY
Python
nums = [1, 2, 3, 4, 5]
nums.append(6)  # grows automatically

2. Slicing

Python's slice syntax nums[start:stop:step] is far more powerful than C's manual index calculations.

PY
Python
nums[1:4]    # elements 1,2,3
nums[::-1]   # reversed copy
nums[::2]    # every other element

3. List Comprehensions

Python list comprehensions replace C's for-loop-with-array patterns with a single expressive line.

C
C
int squares[10];
for (int i = 0; i < 10; i++) squares[i] = i*i;
PY
Python
squares = [i**2 for i in range(10)]

Common Mistakes

When coming from C, developers often make these mistakes:

  • C arrays are fixed-size; Python lists are dynamic
  • C arrays need sizeof tricks for length; Python uses len()
  • No malloc/free in Python — garbage collector handles memory
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

  • Fixed-size C arrays → dynamic Python lists
  • malloc/free → no manual memory management
  • sizeof tricks → len()
  • Python slicing and comprehensions have no C equivalent
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