Lists - 3.2.4

# Lists are abstraction because of their ability to group multiple data types together, without defining them separately
# Lists also allow you to perform operations on them without excessive loops and checking, resulting in simpler code and efficiency
candy = ["Jolly Rancher", "Kit Kat", "Starburst", "Nerds", "Nerd Clusters"]
print(candy)
print("-"*50)

# appending
print("Appending i.e Adding Items to the List:")
print(candy)
print("-"*50)
# abstracts having to manually extend list and inserting element removes that aspect of memory management
candy.append("Laffy Taffy") #adds item to end of list

# removing
print("Removing Element from List:")
print(candy)
print("-"*50)
candy.remove("Kit Kat")

# sorting
print("Sorts lists:")
candy.sort() # removes task of writing sorting algorithm from scratch with high computational efficiency, n log n.
print("Post sort list", candy)

# duplicating
candy_copy = candy.copy() # removes aspect of memory management and manual iteration to create list, and removes appending operations


['Jolly Rancher', 'Kit Kat', 'Starburst', 'Nerds', 'Nerd Clusters']
--------------------------------------------------
Appending i.e Adding Items to the List:
['Jolly Rancher', 'Kit Kat', 'Starburst', 'Nerds', 'Nerd Clusters']
--------------------------------------------------
Removing Element from List:
['Jolly Rancher', 'Kit Kat', 'Starburst', 'Nerds', 'Nerd Clusters', 'Laffy Taffy']
--------------------------------------------------
Sorts lists:
Post sort list ['Jolly Rancher', 'Laffy Taffy', 'Nerd Clusters', 'Nerds', 'Starburst']

Javscript Version

%%js
// Lists in JavaScript (arrays)
let candy = ["Jolly Rancher", "Kit Kat", "Starburst", "Nerds", "Nerd Clusters"];
console.log(candy);
console.log("-".repeat(50));

// Appending (Adding items to the list)
console.log("Appending i.e. Adding Items to the List:");
candy.push("Laffy Taffy");  // Adds item to the end of the list
console.log(candy);
console.log("-".repeat(50));

// Removing element from list
console.log("Removing Element from List:");
let index = candy.indexOf("Kit Kat");
if (index !== -1) candy.splice(index, 1); // Removes "Kit Kat" if it exists
console.log(candy);
console.log("-".repeat(50));

// Sorting the list
console.log("Sorts lists:");
candy.sort(); // removes task of writing sorting algorithm from scratch with high computational efficiency, n log n.
console.log("Post sort list:", candy);

// Duplicating (copying) the list
let candy_copy = [...candy]; // Creates a shallow copy of the list
console.log("Candy copy:", candy_copy);