| Arrays / Lists |
const arr = ["a", "b", "c"];
console.log(arr[10]); // undefined
arr.push("d"); // adds "d" to the end
|
lst = ["a", "b", "c"]
print(lst[10]) # IndexError
lst.append("d") # adds "d" to the end
|
| Slicing |
const lst = [0, 1, 2, 3, 4, 5];
console.log(lst.slice(2, 5));
// [2, 3, 4]
|
lst = [0, 1, 2, 3, 4, 5]
print(lst[2:5])
# [2, 3, 4]
|
| indexOf / includes |
const lst = [0, 1, 2, 3, 4, 5];
console.log(lst.indexOf(3)); // 3
console.log(lst.includes(3)); // true
|
lst = [0, 1, 2, 3, 4, 5]
print(lst.index(3)) # 3
print(3 in lst) # True
|
| Negative Indexing |
const lst = [0, 1, 2, 3, 4, 5];
console.log(lst.at(-1)); // 5
|
lst = [0, 1, 2, 3, 4, 5]
print(lst[-1]) # 5
|
| Copy (Spread Operator) |
const lst = [0, 1, 2, 3, 4, 5];
const copy1 = [...lst];
console.log(copy1 === lst); // false
console.log(copy1);
// [0, 1, 2, 3, 4, 5]
|
nums = [0, 1, 2, 3, 4, 5]
copy1 = [*nums]
print(copy1 is nums) # False
print(copy1)
# [0, 1, 2, 3, 4, 5]
|
| dictionaries - objects |
const obj = { a: 1, b: 2, c: 3 };
console.log(obj.a); // 1
obj.d = 4; // adds new property
delete obj.b; // removes property
console.log(obj.e); // undefined (missing key -> undefined)
console.log(obj["a"]); // 1 (dot or bracket access)
|
obj = { "a": 1, "b": 2, "c": 3 }
print(obj["a"]) # 1
obj["d"] = 4 # adds new property
del obj["b"] # removes property
# print(obj["z"]) # KeyError: 'z'
# (missing key raises, not undefined)
print(obj["a"]) # 1 — always brackets, no dot access
print(obj.get("z", "hello")) # 'hello' — avoids KeyError
|
| entries - items |
for (const [key, val] of Object.entries(dict)) {
console.log(key, val);
}
|
for key, val in d.items():
print(key, val)
|
| Tuples - (immutable — Python-specific) |
|
tup = (1, 2, 3)
print(tup[1]) # 2
# tup[0] = 5 # TypeError:
#'tuple' object does not support item assignment
# GOTCHA: a trailing comma makes a tuple
#(often a typo!)
a = 1
print(type(a)) # class 'int'
a = 1,
print(type(a)) # class 'tuple'
|