Lists, Tuples, and Slicing
Master Python list operations, slicing syntax, and tuple immutability with hands-on examples drawn from classic coding challenges.
Lists, Tuples, and Slicing is a free DSA 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 DSA Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Python Lists: Dynamic Arrays
A Python list is a dynamic array that holds anything and grows on its own. It is ordered, changeable, and gives you instant O(1) access by index. The code shows the basics.
nums = [3, 1, 4, 1, 5]
print(nums[0]) # 3
print(nums[-1]) # 5 (last element)
nums.append(9)
print(len(nums)) # 6Common List Operations
Know these by heart: append and pop at the end are O(1), but insert at the front is O(n). Avoid remove in tight loops — it rescans every time.
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
print(stack.pop()) # 3 O(1)
print(stack) # [1, 2]
# insert at index 0 is O(n)
stack.insert(0, 0)
print(stack) # [0, 1, 2]Slicing Syntax Explained
Slicing reads as lst[start:stop:step] and stops just before stop. The classic trick: a step of -1 reverses a list without changing it. The code walks through each form.
a = [0, 1, 2, 3, 4, 5]
print(a[1:4]) # [1, 2, 3]
print(a[:3]) # [0, 1, 2]
print(a[3:]) # [3, 4, 5]
print(a[::2]) # [0, 2, 4] every other
print(a[::-1]) # [5, 4, 3, 2, 1, 0] reversedSlicing Creates Shallow Copies
A handy gotcha: slicing always makes a new list, so editing the slice leaves the original alone. But it is shallow, so nested lists inside are still shared. Watch out.
original = [1, 2, 3]
copy = original[:] # shallow copy
copy[0] = 99
print(original) # [1, 2, 3] unchanged
# Nested list pitfall
nested = [[1, 2], [3, 4]]
shallow = nested[:]
shallow[0][0] = 99
print(nested) # [[99, 2], [3, 4]] changed!Tuples: Immutable Sequences
A tuple uses parentheses and cannot be changed once made. Because it is hashable, it can be a dict key or set member — perfect for (row, col) pairs in grid problems.
point = (3, 7)
print(point[0]) # 3
# Use as dict key
grid = {}
grid[(0, 0)] = 'start'
grid[(2, 3)] = 'end'
print(grid[(0, 0)]) # start
# Unpacking
x, y = point
print(x, y) # 3 7When to Use Tuple vs List
Pick a tuple when the data should not change, and a list when it should. Tuples use a bit less memory too, which adds up inside big heaps in Dijkstra.
import sys
a_list = [1, 2, 3, 4, 5]
a_tuple = (1, 2, 3, 4, 5)
print(sys.getsizeof(a_list)) # e.g. 104 bytes
print(sys.getsizeof(a_tuple)) # e.g. 80 bytes
# Tuple returned from function
def min_max(nums):
return min(nums), max(nums)
lo, hi = min_max([3, 1, 4, 1, 5])
print(lo, hi) # 1 5Off-by-One Errors in Slicing
Off-by-one bugs are the top trap in array problems. Remember lst[i:j] gives exactly j-i items. To split at the middle, left is lst[:mid] and right is lst[mid:].
lst = [1, 2, 3, 4, 5, 6]
mid = len(lst) // 2 # 3
left = lst[:mid] # [1, 2, 3]
right = lst[mid:] # [4, 5, 6]
print(left, right)
# How many elements?
print(len(lst[2:5])) # 3 (indices 2,3,4)Negative Indices in Interviews
Python has negative indices: lst[-1] is the last item, lst[-2] the one before. Prefer lst[-1] over lst[len(lst)-1] — interviewers notice the cleaner version.
s = 'abcde'
print(s[-1]) # 'e'
print(s[-3:]) # 'cde'
print(s[:-2]) # 'abc' (all except last 2)
nums = [10, 20, 30, 40]
print(nums[-2]) # 30
nums[-1] = 99
print(nums) # [10, 20, 30, 99]List Sorting and Comparison
Python sorting uses Timsort (O(n log n), stable). lst.sort() changes the list in place; sorted(lst) returns a new one. Use the key option for custom order. See the code.
words = ['banana', 'fig', 'apple', 'kiwi']
words.sort(key=len)
print(words) # ['fig', 'kiwi', 'apple', 'banana']
nums = [3, 1, 4, 1, 5]
print(sorted(nums, reverse=True)) # [5, 4, 3, 1, 1]
print(nums) # unchanged: [3, 1, 4, 1, 5]Mutating Lists During Iteration
A classic trap: never add or remove items while looping over a list — you will skip elements or crash. Build a new list with a comprehension instead.
# WRONG — skips elements
nums = [1, 2, 3, 4, 5]
for n in nums:
if n % 2 == 0:
nums.remove(n) # skips 4!
print(nums) # [1, 3, 5]... but 4 got skipped? Actually removes 2,4
# CORRECT — build new list
nums = [1, 2, 3, 4, 5]
nums = [n for n in nums if n % 2 != 0]
print(nums) # [1, 3, 5]Tuple Unpacking in Loops
Tuple unpacking keeps loops clean: enumerate gives (index, value) and zip walks two lists together. The *rest syntax grabs the tail into its own variable. See the code.
nums = [10, 20, 30]
for i, v in enumerate(nums):
print(i, v)
# 0 10 / 1 20 / 2 30
a = [1, 2, 3]
b = ['x', 'y', 'z']
for x, y in zip(a, b):
print(x, y)
first, *rest = [1, 2, 3, 4]
print(first, rest) # 1 [2, 3, 4]Quick Check
Quick check — show what you have picked up about Python lists, tuples, and slicing. You have got this. 💪
Lesson Recap
Recap: lists are dynamic arrays with O(1) append, slicing always returns a fresh copy, and tuples are immutable and hashable. Next up: dictionaries and sets.
Frequently asked questions
Is the “Lists, Tuples, and Slicing” lesson free?
Yes — the full text of “Lists, Tuples, and Slicing” is free to read here on the web, and the DSA 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 DSA Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Lists, Tuples, and Slicing”?
Master Python list operations, slicing syntax, and tuple immutability with hands-on examples drawn from classic coding challenges. You practise DSA 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 DSA Interview Prep?
No prior experience is required. DSA 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, Tuples, and Slicing” 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 DSA Interview Prep lesson?
Yes. Every DSA 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
- Lists, Tuples, and Slicing
- Dictionaries and Sets in Python
- Comprehensions and Built-ins
- Functions, Closures, and Lambda