0Pricing
Coding Interview Prep · Lesson

Lists, Indexing & Slicing for CP

Access, slice, and reverse without off-by-one.

Lists, Indexing & Slicing for CP is a free Coding Interview Prep lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Your Contest Workhorse

In competitive Python, the list is your go-to array. It stores items in order and grows as needed, so most problems start here.

a = [4, 1, 7, 3]

Indexing from Zero

Lists use zero-based indexing, so a[0] is the first item. Forgetting this is the classic off-by-one bug that costs easy points.

a = [4, 1, 7, 3]
print(a[0])  # 4

Reach the End Backwards

Negative indices count from the right, so a[-1] is the last element. No need to compute len(a) - 1 yourself.

a = [4, 1, 7, 3]
print(a[-1])  # 3

How Long Is It?

Use len(a) to get the element count. Valid indices then run from 0 up to len(a) - 1, never further.

a = [4, 1, 7, 3]
print(len(a))  # 4

Grab a Slice

A slice a[i:j] takes items from index i up to but not including j. The right end is always excluded.

a = [4, 1, 7, 3]
print(a[1:3])  # [1, 7]

Open-Ended Slices

Leave a side blank to reach the edge. a[:k] takes the first k items and a[k:] takes everything from k onward.

a = [4, 1, 7, 3]
print(a[:2], a[2:])

Step Through with Stride

A third number is the step. a[::2] takes every second element, handy for splitting into even and odd positions.

a = [4, 1, 7, 3]
print(a[::2])  # [4, 7]

Reverse in One Move

A step of -1 flips the list. a[::-1] gives a reversed copy without touching the original, great for palindromes.

a = [4, 1, 7, 3]
print(a[::-1])  # [3, 7, 1, 4]

Slices Make Copies

Slicing returns a fresh list, so a[:] is a quick shallow copy. Editing the copy will not change the original array.

b = a[:]
b[0] = 99  # a is untouched

Safe Bounds Beat Crashes

Reading a[len(a)] raises an IndexError, an instant runtime error verdict. Always check that an index sits inside the list.

if i < len(a):
    use(a[i])

Slicing Never Crashes

Unlike indexing, an out-of-range slice is forgiving. a[2:99] just returns whatever exists, so it quietly clips to the end.

a = [4, 1, 7, 3]
print(a[2:99])  # [7, 3]

Quick Check

Test your slicing instincts on a small list.

Recap: Lists in Hand

You can now index from both ends, slice ranges, step, and reverse safely. These list basics are the foundation for every array problem ahead. 🚀

Frequently asked questions

Is the “Lists, Indexing & Slicing for CP” lesson free?

Yes — the full text of “Lists, Indexing & Slicing for CP” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Lists, Indexing & Slicing for CP”?

Access, slice, and reverse without off-by-one. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Lists, Indexing & Slicing for CP” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lists, Indexing & Slicing for CP
  2. Build Arrays Fast with Comprehensions
  3. Min, Max, Sum & Running Totals
  4. Find the Index, Not Just the Value
← Back to Coding Interview Prep