Data StructuresLesson 1 of 3
Lists (= Arrays)
Python's list is your JS array. Same square bracket syntax, same zero-based indexing. You will feel right at home until you call .push() and get an AttributeError. It is .append() in Python.
The killer feature JS arrays do not have is slicing. With my_list[1:4], you grab elements 1 through 3 in a single expression. Add a step value (my_list[::2] for every other element) or go negative (my_list[::-1] to reverse). Negative indexes count from the end, so my_list[-1] is the last item. Once slicing clicks, you will wonder how you ever lived without it.
Watch out for a few common traps. First: list.sort() sorts in place and returns None, not the sorted list. If you write sorted_list = my_list.sort(), sorted_list is None. Use sorted(my_list) to get a new sorted list. Second: multiplying a list repeats references to the same objects. [[0]] * 3 gives you three references to the SAME inner list, so changing one changes all of them. Use a comprehension instead: [[0] for _ in range(3)].
Slicing always creates a new list, but it is a shallow copy. So b = a[:] (or a.copy()) copies the list, while nested objects inside are still shared between the original and the copy. For deep copies, use copy.deepcopy(), the equivalent of JS's structuredClone().
Destructuring works too, with a slightly different syntax. Instead of const [first, ...rest] = arr, you write first, *rest = arr. The star (*) replaces the spread dots, and rest is always a list.
Quick reference
| JavaScript | Python | Note |
|---|---|---|
| .push(x) | .append(x) | Most common gotcha |
| .push(...items) | .extend(items) | Add many items |
| .unshift(x) | .insert(0, x) | Insert at any index |
| .includes(x) | x in lst | in operator |
| .indexOf(x) | .index(x) | Raises ValueError if missing! |
| .splice(i, 1) | del lst[i] / lst.pop(i) | Remove by index |
| [...arr] | [*arr] / arr.copy() | Shallow copy |
| arr.slice(1, 3) | arr[1:3] | Bracket slicing |
| arr.at(-1) | arr[-1] | Negative indexing built in |
Side by side
// JavaScript Arrays
const nums = [1, 2, 3, 4, 5];
nums.push(6);
nums.pop();
nums.unshift(0);
nums.includes(3);
nums.indexOf(3);
nums.splice(1, 1);
nums.slice(1, 3);
nums.at(-1);
const [first, second, ...rest] = nums;
const combined = [...nums, 7, 8];# Python Lists
nums = [1, 2, 3, 4, 5]
nums.append(6) # push -> append
nums.pop() # same!
nums.insert(0, 0) # unshift -> insert
3 in nums # includes -> in
nums.index(3) # indexOf -> index
del nums[1] # splice -> del
nums[1:3] # slice syntax
# Slicing superpowers
nums[::2] # every other element
nums[::-1] # reversed copy
nums[-1] # last element
nums[-3:] # last 3 elements
first, second, *rest = nums # unpack
combined = [*nums, 7, 8] # spreadExercise
Real Python, running in your browserWrite Python code that does the following:
['apple', 'cherry', 'date'] ['fig', 'elderberry', 'date', 'cherry', 'apple']
Ctrl + Enter runCtrl + Shift + Enter checkTab indentEsc then Tab to leave the editorCtrl + / comment