Lists:

Lists are similar to its name, it’s a variable that equals a list of objects inside of it. For example, if I want to make a list of ingredients, I can make ingredients equal something inside these “[]”. Inside these brackets, you can put different objects inside of it and print out the list to be able to get everything inside that list.

Example:

list1 = [“Eggs”, “Bacon”, “rice”] print(list1)

Lists with Indexs:

If you want to print out specific parts inside that list, we can do that by printing out the index. From the index, it goes from index (0) -> index (1) -> index (2) -> etc.

For example, if I wanted to print out the words “eggs” only from a list, I would mention its index inside the list AND the list, and print it out:

Example:

list1 = [“Eggs”, “Bacon”, “rice”] print(list1[0])

Getting Multiple Indexs In A List:

If you want to get multiple indexes from the same list without making multiple print statements/etc. you can use a “:” to scale it from the starting point, to how many objects it should contain. For example,

text[0:1] will only print out “EGG” because it gets index “0” and gets only ONE object

text[0:2] will print out “EGG”, “BACON” because it gets index “0” and gets TWO objects

Example:

list1 = [“Eggs”, “Bacon”, “rice”] print(list1[0:2])

Appending something:

#MAIN LIST:

list1 = ["Eggs", "Bacon", "rice"]

#This prints out the ENTIRE list

print(list1)

#This prints out only one part of the list
#in this case, the list goes from 0 -> 1 -> 2 -> etc, so it'll print out "Bacon"

print(list1[1]) 

#This prints out 2 parts of the list
#in this case, the list goes from 0 -> 1 -> 2 -> etc, so it'll print out "eggs", "bacon"

print(list1[0:2]) 

#Appending a list, (Note: this affects the list entirely untill its removed):

list1.append("served")
print(list1)

#removing the "served" from the list

list1.remove(list1[3]) #You can also use indexes for these as well. Helpful especially if the name is long.
print(list1)

Javascript version:

The javascript version is a bit different. It prints out the same properties, but the different commands are written differently, plus you need to have “;” at the end of everything.

Changes in List:

The lists is mostly similar to the python code, except its “let x = [“a”, “b”, “c”]” instead of it being “x = [“a”, “b”, “c”]

Changes in prints

The print statements just get changed from “print” to “console.log” so it prints it in the console instead of a python server.

Multiple Indexes:

The code for multiple indexes is a bit different in javascript. Instead of using [0:2] you have to use a “slice” along with using paranthesses. For example:

let x = [“a”, “b”, “c”]; console.log(x.slice(0,2));

Appending

For appending, you instead use “.push” instead of “.append”

Removing

For removing, you use “.splice” with the index you want to move it in, and how many indexes you want to replace.

let x = [“a”, “b”, “c”]

x.push(“d”) console.log(x)

x.splice(3, 1, “e”) //This replaces “d” with “e” console.log(x)

x.splice(3, 1) //This just removes “e” console.log(x)

%%js

// MAIN LIST
let list1 = ["Eggs", "Bacon", "Rice"];

// This prints out the ENTIRE list
console.log(list1);

// This prints out only one part of the list
// In this case, the list goes from 0 -> 1 -> 2 -> etc., so it'll print out "Bacon"
console.log(list1[1]);

// This prints out 2 parts of the list
// In this case, it'll print out "Eggs", "Bacon"
console.log(list1.slice(0, 2)); // JavaScript slice() method extracts a portion of the array

// Appending a list (Note: this affects the list entirely until it's removed)
list1.push("Served");
console.log(list1);

// Removing "Served" from the list
list1.splice(3, 1); // Using index to remove element at position 3
console.log(list1);

Popcorn Hack: Lists


Step 1: Create a List

Create a list of numbers: [3, 5, 7, 9, 11].

Step 2: Access an Element

Access the third element of the list.

Step 3: Modify an Element

Change the second element to 6.

Step 4: Add an Element

Append 13 to the list.

Step 5: Remove an Element

Remove the element 9 from the list.

Bonus:

Sort the list in descending order.