📘 Lesson: Lists in Python & JavaScript


1. What is a List?

  • Python → Lists are used to store multiple items in a single variable.
  • JavaScript → Similar idea, but they’re usually called arrays.
  • They can store numbers, text, or even mixed types
  • Think of them like a row of labeled boxes where each box holds a value

2. Creating a List

  • A list/array is made with square brackets []
  • Values are separated by commas
  • Both Python and JavaScript allow mixed types in the same list

Python

# A list of numbers
numbers = [1, 2, 3, 4, 5]

# A list of mixed types
mixed = ["apple", 10, True]

Javascript

// A list (array) of numbers
let numbers = [1, 2, 3, 4, 5];

// A list of mixed types
let mixed = ["apple", 10, true];

3. Accessing Items

  • Each item has a position called an index
  • Indexing starts at 0 (so the first item is position 0)
  • You can use the index to read the value at that position

Python

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # apple
print(fruits[2])  # cherry

Javascript

let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // apple
console.log(fruits[2]); // cherry

4. Changing Items

  • You can replace values in a list/array
  • Select the index and assign a new value
  • Example: change “banana” to “blueberry”

Python

fruits[1] = "blueberry"
print(fruits)  # ['apple', 'blueberry', 'cherry']

Javascript

fruits[1] = "blueberry";
console.log(fruits); // ['apple', 'blueberry', 'cherry']

5. Adding Items

  • Lists/arrays can grow by adding new values
  • Add to the end with append (Python) or push (JavaScript)
  • Insert at a specific position with insert (Python) or splice (JavaScript)

Python

fruits.append("orange")   # add to end
fruits.insert(1, "grape") # add at position

Javascript

fruits.push("orange");        // add to end
fruits.splice(1, 0, "grape"); // add at position

6. Removing Items

  • Lists/arrays can shrink by removing values
  • Remove by value (Python) or by index (JavaScript)
  • Remove the last item with pop in both languages

Python

fruits.remove("apple")  # remove by value
fruits.pop()            # remove last item

Javascript

fruits.splice(0, 1);   // remove by index
fruits.pop();          // remove last item

7. Looping Through a List

  • Looping means going through items one by one
  • Useful for printing, checking, or processing each value
  • Both Python and JavaScript have simple loop syntax

Python

for fruit in fruits:
    print(fruit)

Javascript

for (let fruit of fruits) {
    console.log(fruit);
}

8. Key Differences

  • Python lists and JavaScript arrays work almost the same
  • Python has built-in functions like append, insert, remove
  • JavaScript arrays often use methods like push, splice, pop
  • Syntax is very similar across both