Functions and data structures · Part 8 of 36
Lists, dictionaries and objects
Basic 15 min
Store many values in arrays/lists and key–value maps, and transform them with map, filter and comprehensions.
What you will learn
- You can create, read and change lists (arrays).
- You can use dictionaries (Python) and objects/Maps (JavaScript) for key–value data.
- You can transform collections with map, filter and comprehensions.
Before this: Functions: name a piece of work
Lists (arrays): values in order
A list — called an array in JavaScript — keeps values in order, each at an index starting at 0.
const harvest = [12, 7, 30];
harvest.push(5); // add at the end → [12, 7, 30, 5]
harvest[1] = 8; // change → [12, 8, 30, 5]
harvest.length; // 4
harvest.includes(30); // true
harvest = [12, 7, 30]
harvest.append(5) # [12, 7, 30, 5]
harvest[1] = 8 # [12, 8, 30, 5]
len(harvest) # 4
30 in harvest # True
Lists can be changed in place (they are mutable). Python also has tuples, (12, 7), which cannot change — handy for fixed pairs such as coordinates.

Dictionaries and objects: values by name
When you look things up by a name rather than a position, use a key–value structure:
const plant = { name: "basil", waterMl: 150, sunny: true };
plant.name; // "basil"
plant["waterMl"]; // 150
plant.height = 30; // add a key
plant = {"name": "basil", "water_ml": 150, "sunny": True}
plant["name"] # "basil"
plant.get("height", 0) # 0 — default if the key is missing
plant["height"] = 30
In JavaScript, plain objects use string keys; for keys of any type or frequent adding/removing, use a Map. Python's dict accepts any immutable key and keeps insertion order (guaranteed since Python 3.7).
A set stores unique values without order: new Set([1, 2, 2]) and {1, 2, 2} both contain just 1 and 2. Sets answer "is x in here?" very quickly.
References: two names, one list
Assigning a list to another variable does not copy it:
a = [1, 2]
b = a
b.append(3)
print(a) # [1, 2, 3] — a and b are the same list
JavaScript behaves the same way with arrays and objects. Copy explicitly when you need independence: [...a] or structuredClone(a) in JavaScript, a.copy() or copy.deepcopy(a) in Python.
Transforming collections
Instead of writing loops by hand, you often describe the transformation:
const prices = [2, 5, 9];
const withTax = prices.map((p) => p * 1.2); // [2.4, 6, 10.8]
const cheap = prices.filter((p) => p < 6); // [2, 5]
const total = prices.reduce((sum, p) => sum + p, 0); // 16
prices = [2, 5, 9]
with_tax = [p * 1.2 for p in prices] # list comprehension
cheap = [p for p in prices if p < 6]
total = sum(prices)
Python's list comprehension [expression for item in list if condition] combines map and filter in one readable line. Sorting: prices.sort((a, b) => a - b) in JavaScript (without the compare function it sorts as text, so 10 would come before 9!), sorted(prices) in Python.
Nesting
Real data nests: a list of dictionaries, a dictionary of lists. beds[0]["plants"][2] reads "first bed, its plants, the third one". When you print nested data, it starts to look like JSON — the topic of the next lesson.
Try it: JavaScript sandbox
Write and run small JavaScript programs; the checker tells you when an exercise is solved.
Turn on JavaScript to use this lab. Worked example:
Exercise 8: given const temps = [3, 12, -2, 8], print only the temperatures above 0, doubled, as an array: [6, 24, 16]. Use filter and map.
Key points
- Lists/arrays keep values in order; indexes start at 0; they are mutable.
- Dictionaries (Python) and objects/Maps (JavaScript) store values by key; sets store unique values.
- Assignment copies the reference, not the list — copy explicitly when needed.
- map, filter and reduce (JavaScript) or comprehensions and sum (Python) transform collections.
- JavaScript's sort() without a compare function sorts numbers as text.
Flashcards
- Question: How do you add an item to the end of a list?
- Answer: push() in JavaScript, append() in Python.
- Question: What does plant.get("height", 0) do in Python?
- Answer: Returns the value for "height", or 0 if the key is missing.
- Question: Why does b.append(3) also change a after b = a?
- Answer: Both names refer to the same list object.
- Question: What does [p for p in prices if p < 6] produce?
- Answer: A new list with only the prices below 6.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
What does [10, 9, 1].sort() return in JavaScript?
- [1, 10, 9]
- [1, 9, 10]
- [10, 9, 1]
- an error
Show the answer
[1, 10, 9]
Without a compare function, sort converts values to strings and sorts them as text: "1" < "10" < "9".
Source: MDN, Array.prototype.sort()
Which Python structure keeps only unique values?
- set
- list
- tuple
- str
Show the answer
set
A set stores each value at most once.
Source: Python docs, 5.4 Sets
In Python, [x * 2 for x in nums] is called a list ___.
Show the answer
comprehension
List comprehensions build a new list from an expression and a loop.
Source: Python docs, 5.1.3 List Comprehensions
prices = [2, 5, 9]. What is sum(p for p in prices if p > 3)?
Show the answer
14
Only 5 and 9 are above 3: 5 + 9 = 14.
Source: Python docs, Data Structures
Which create an independent copy of the JavaScript array a?
- [...a]
- structuredClone(a)
- const b = a
- a.slice()
Show the answer
[...a]; structuredClone(a); a.slice()
Spread, slice() and structuredClone create new arrays; assignment only copies the reference.
Source: MDN, Array
Sources
- MDN Web Docs JavaScript Guide, Mozilla, 2025
- The Python Tutorial (3.14), Python Software Foundation, 2025
- Python documentation: Data Structures, Python Software Foundation, 2025
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.