Creating Lists
Key Points
Define lists with []
Store multiple values
Mix data types
JavaScript
let colors = ["red", "green", "blue"];
console.log(colors);
In JavaScript, you create a list (array) using [].
- let colors = ["red", "green", "blue"]; creates an array called colors with three items.
- Arrays can hold numbers, strings, or other arrays.
- console.log(colors); prints the array to the console.
Arrays are a flexible way to store groups of values in JavaScript.
Python
colors = ["red", "green", "blue"]
print(colors)
In Python, you create a list using [].
- colors = ["red", "green", "blue"] creates a list called colors with three items.
- Lists can hold numbers, strings, or other lists.
- print(colors) displays the list.
Lists are a basic way to store and organize data in Python.
Accessing Elements
Key Points
Use indexes to access items
Start counting at 0
Access first, last, or middle elements
JavaScript
let animals = ["cat", "dog", "bird"];
console.log(animals[1]); // dog
console.log(animals[animals.length - 1]); // bird
JavaScript arrays are zero-indexed, meaning the first item is at position 0.
- animals[1] gets the second item ("dog").
- animals[animals.length - 1] gets the last item ("bird").
Use indexes to access any element in the array.
Python
animals = ["cat", "dog", "bird"]
print(animals[1]) # dog
print(animals[-1]) # bird
Python lists are zero-indexed, so the first item is at position 0.
- animals[1] gets the second item ("dog").
- animals[-1] gets the last item ("bird").
Use indexes to access any element in the list.
Modifying Lists
Key Points
Change values by index
Update existing items
Keep lists dynamic
JavaScript
let scores = [10, 20, 30];
scores[0] = 15;
scores[2] = scores[0] + scores[1];
console.log(scores); // [15, 20, 35]
You can change an array item by assigning a new value to its index.
- scores[0] = 15; replaces 10 with 15.
- scores[2] = scores[0] + scores[1]; updates the last item to the sum of the first two.
Arrays are dynamic and can be changed anytime.
Python
scores = [10, 20, 30]
scores[0] = 15
scores[2] = scores[0] + scores[1]
print(scores) # [15, 20, 35]
You can change a list item by assigning a new value to its index.
- scores[0] = 15 replaces 10 with 15.
- scores[2] = scores[0] + scores[1] updates the last item to the sum of the first two.
Lists are dynamic and can be changed anytime.
Adding Elements
Key Points
Use append/push to add to the end
Insert at specific positions
Grow lists dynamically
JavaScript
let cities = ["Paris", "London"];
cities.push("Tokyo");
cities.splice(1, 0, "Berlin");
console.log(cities); // ["Paris", "Berlin", "London", "Tokyo"]
- push() adds an item to the end of the array.
- splice() can insert items at any position.
- console.log(cities); prints the final array.
Arrays can grow as you add more items.
Python
cities = ["Paris", "London"]
cities.append("Tokyo")
cities.insert(1, "Berlin")
print(cities) # ["Paris", "Berlin", "London", "Tokyo"]
- append() adds an item to the end of the list.
- insert() can add items at any position.
- print(cities) prints the final list.
Lists can grow as you add more items.
Removing Elements
Key Points
Remove by value
Remove by index
Pop the last element
JavaScript
let colors = ["red", "green", "blue", "yellow"];
colors.splice(2, 1); // Remove item at index 2 ("blue")
colors.pop(); // Remove last item ("yellow")
console.log(colors); // ["red", "green"]
- splice() removes items by index.
- pop() removes the last item.
- console.log(colors); prints the updated array.
You can remove items from arrays by value or index.
Python
colors = ["red", "green", "blue", "yellow"]
colors.remove("blue")
colors.pop()
print(colors) # ["red", "green"]
- remove() removes an item by value.
- pop() removes the last item.
- print(colors) prints the updated list.
You can remove items from lists by value or index.
Looping Through Lists
Key Points
Use for loops to iterate
Process each element
Use for…of in JavaScript
JavaScript
let animals = ["cat", "dog", "bird"];
// Complete traversal
for (let animal of animals) {
console.log(animal);
}
// Partial traversal
for (let i = 0; i < 2; i++) {
console.log(animals[i]);
}
- for...of loops go through each item in the array.
- Use a regular for loop for partial traversal.
Looping lets you process every element in an array.
Python
animals = ["cat", "dog", "bird"]
# Complete traversal
for animal in animals:
print(animal)
# Partial traversal
for i in range(0, 2):
print(animals[i])
- for loops go through each item in the list.
- Use range() for partial traversal.
Looping lets you process every element in a list.
Try It Yourself: Print the First Item
Key Points
Practice accessing elements in a list or array
Remember: Indexing starts at 0 in both Python and JavaScript
Try It Yourself (JavaScript)
Print the first fruit in the list using console.log and array indexing. The expected output should be 'apple'.
Try It Yourself (Python)
Print the first fruit in the list using print and list indexing. The expected output should be 'apple'.