List Operations: aList[i] - This access your list at index i. An index is a numeric value that represents the position of an element within that data structure. For example, the first element of aList is at index 1, represented by aList[1]. x <- aList[i] - Assigns value of aList[i] to variable x aList[i] <- x - Assigns value of x to aList[i] aList[i] <- aList[j] - Assigns value of aList[j] to aList[i]

INSERT(aList, i , value) - aList is the list in which you want to insert the value. i is the index at which you want to insert the value. value is the value you want to insert at that index APPEND(aList, value) - The value you put in is placed at the end of aList REMOVE(aList, i) - aList is the list in which you want to delete the value. i is the index at which you want to delete the value. LENGTH(aList) - Gives you the number of elements in aList

FOR EACH item IN aList { } - Item is a var assigned each element of aList in order from first element to last. The code inside the for loop is run once for every assignment of item.

Append: To add an element at the end of the list

aList ← []

USER_INPUT ← (“Enter an item you want (or ‘q’ to quit): “)

REPEAT UNTIL USER_INPUT ← q{ APPEND (aList, USER_INPUT) }

DISPLAY(aList)

%%js
let fruits = ['apple', 'banana', 'cherry'];
fruits.push('orange');  // Appends 'orange' to the end of the array

console.log(fruits);  
// Output: ['apple', 'banana', 'cherry', 'orange']

Popcorn Hack If you wanted to go backwards, indexing works slightly differently. For instance, in a list of numbers such as (10, 20, 30, 40) if you wanted to access the number 40 specifically you would reference it using a negative index. In this case, you would use -1 enclosed in brackets when referencing it.

Accessing an element:

%%js 
let aList = [];

while (true) {
    let user_input = prompt("Enter an item you want (or 'q' to quit):");
    
    if (user_input.toLowerCase() === 'q') {
        break;
    }
    
    aList.push(user_input);
}

console.log("The second thing on your list is: " + aList[1]);
console.log(aList);
console.log("Here is a range of data: ", aList.slice(1, 3));  // JavaScript equivalent of Python's list slicing



aList = []

while True:
    user_input = input("Enter an item you want (or 'q' to quit): ")
    
    if user_input.lower() == 'q':
        break
    
    aList.append(user_input)

# Displaying the second element (index 1)
if len(aList) > 1:
    print("The second thing on your list is", aList[1])
else:
    print("There is no second item in your list.")

# Display a range of data from index 1 to 3 (non-inclusive)
print("Here is a range of data (index 1 to 3):", aList[1:3])

Deleting An Element:

%%js 
let aList = [];

while (true) {
    let user_input = prompt("Enter an item you want (or 'q' to quit):");
    
    if (user_input.toLowerCase() === 'q') {
        break;
    }
    
    aList.push(user_input);
}

console.log("This is your list: ", aList);

// Remove the second item (index 1)
aList.splice(1, 1);  // Equivalent of `del aList[1]` in Python

console.log("This is your new list: ", aList);

aList = []

while True:
    user_input = input("Enter an item you want (or 'q' to quit): ")
    
    if user_input.lower() == 'q':
        break
    
    aList.append(user_input)

print("This is your list:", aList)

# Deleting the second item (index 1)
if len(aList) > 1:
    del aList[1]
    print("This is your new list after deleting the second item:", aList)
else:
    print("There is no second item to delete.")

Accessing Element Excersises: Problem: Write a Python program that:

1.Prompts the user to enter 5 items (strings) into a list. 2.Asks the user to input an index number. 3.The program should display the item at the index number provided by the user. 4.Additionally, display the range of items from index 1 to 4 (non-inclusive).

Problem: Write a JavaScript program that:

Prompts the user to enter 5 items (strings) into an array. Asks the user to input an index number. The program should display the item at the index number provided by the user. Additionally, display the range of items from index 1 to 4 (non-inclusive).

Deleting Element Excersises: Problem: Write a Python program that:

Prompts the user to enter 5 items (strings) into a list. Asks the user for the index of the item they want to delete. The program should delete the item at that index and print the updated list.

Problem: Write a JavaScript program that:

Prompts the user to enter 5 items (strings) into an array. Asks the user for the index of the item they want to delete. The program should delete the item at that index and print the updated array.

%%js
let aList = ["Yeezys", "Yeezys"];
let bList = ["No Yeezys"];

bList = aList; // Assigning the values of aList to bList
let listLength = bList.length; // Getting the length of the list

console.log("Things I want:", bList);
console.log("How many Yeezys:", listLength);
%%js 
let randomList = [1, 2, 3, 4, 5];
console.log("Original List:", randomList);

randomList[2] = 100; // Modify the item at index 2 (third element)
console.log("Modified List:", randomList);

aList = ["Yeezys", "Yeezys"]
bList = ["No Yeezys"]

bList = aList  # Assigning the values of aList to bList
list_length = len(bList)  # Getting the length of the list

print("Things I want:", bList)
print("How many Yeezys:", list_length)
random_list = [1, 2, 3, 4, 5]
print("Original List:", random_list)

random_list[2] = 100  # Modify the item at index 2 (third element)
print("Modified List:", random_list)

Checking if an element exists: In python, you can use the in keyword, which will return true if the element is found in the list.

my_list = [10, 20, 30, 40, 50]

# Check for an element
element_to_check = 30

if element_to_check in my_list:
    print(f"{element_to_check} is in the list.")
else:
    print(f"{element_to_check} is not in the list.")
%%js
let fruits = ["apple", "banana", "orange"];

if (fruits.includes("banana")) {
    console.log("Banana is in the list!");
} else {
    console.log("Banana is not in the list.");
}

List Hacks: Sum of Numbers sum -> 10 nums ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_sum ← 0

FOR EACH score IN nums IF score MOD 2 = 0 THEN even_sum ← even_sum + score END IF END FOR

DISPLAY (“Sum of even numbers in the list:”, even_sum)

%%js
let numbers = [1, 2, 3, 4, 5, 6];
let sum = 0;

for (let num of numbers) {
    if (num % 2 === 0) {
        sum += num;
    }
}

console.log("Sum of even numbers:", sum);
Hacks: Successfully find a specific element in a Lists
Find the maximum and minimum value of a list
def sum_of_odd_numbers(numbers):
    return sum(num for num in numbers if num % 2 != 0)

# Example usage:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(sum_of_odd_numbers(numbers))  # Output: 25
%%js
function sumOfOddNumbers(numbers) {
    let sum = 0;
    for (let num of numbers) {
        if (num % 2 !== 0) {
            sum += num;
        }
    }
    return sum;
}

// Example usage:
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(sumOfOddNumbers(numbers));  // Output: 25
Mini Quiz

Write a Python program that creates a list of the following numbers: 10, 20, 30, 40, 50. Then, print the second element in the list.
Write a JavaScript program that creates an array of the following numbers: 10, 20, 30, 40, 50. Then, log the second element in the array to the console.
Python:
Given the list fruits = ['apple', 'banana', 'cherry'], add 'orange' to the end of the list.
JavaScript:
Given the array let colors = ['red', 'green', 'blue', 'yellow'], remove the element 'blue' from the array.