List examples in Python, JavaScript, and Pseudocode.

fruits = ["apple", "grape", "strawberry"]
print (fruits)
#const fruits = ["apple", "grape", "strawberry"];
#fruits ← [apple, grape, strawberry]
['apple', 'grape', 'strawberry']
# change the value "soccer" to "hockey"
#print (sports)

sports = ["football", "hockey", "baseball", "basketball"]

# change the value "soccer" to "hockey"
print (sports)
['football', 'hockey', 'baseball', 'basketball']
# add "golf" as the 3rd element in the list
#print (sports)

sports = ["football", "soccer", "golf", "baseball", "basketball"]

# add "golf" as the 3rd element in the list
print (sports)
['football', 'soccer', 'golf', 'baseball', 'basketball']

Determine output of doe segment

display words outputs ["unusual", "bold", "away"] because those words dont have a length of 3 letters

Iteration

The repetition of a process. Things like for loops and while loops. Iteration means you dont have to something like what is shown below, but instead could just iterate through a list.

Lists, tuples, dictionaries, and sets are iterable objects. They are the 'containers' that store the data to iterate.

Each of these containers are able to iterate with the iter() command.

There are 2 types of iteration:definite and indefinite. Definite iteration clarifies how many times the loop is going to run, while indefinite specifies a condition that must be met

print("alpha")
print("bravo")
print("charlie")
print("delta")
print("echo")
print("foxtrot")
print("golf")
print("hotel")
print("india")
print("juliett")
print("kilo")
print("lima")
print("mike")
print("november")
print("oscar")
print("papa")
print("quebec")
print("romeo")
print("sierra")
print("tango")
print("uniform")
print("victor")
print("whiskey")
print("x-ray")
print("yankee")
print("zulu")

Iterator? Iterable? Iteration?

  • When an object is iterable it can be used in an iteration
  • When passed through the function iter() it returns an iterator
  • Strings, lists, dictionaries, sets and tuples are all examples of iterable objects.
a = ['alpha', 'bravo', 'charlie']

itr = iter(a)
print(next(itr))
print(next(itr))
print(next(itr))
alpha
bravo
charlie

Loops

  • Well, above is basically just printing them again, so how do we takes these iterators into something we can make use for?
  • Loops take essentially what we did above and automates it, here are some examples.
list = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee", "Zulu"]

# using a for loop 
for i in list:
    #for item in the list, print the item 
    print(i)
    
list = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee", "Zulu"]

# Taking the length of the list 
lengthList = len(list) 

# Iteration using the amount of items in the list
for i in range(lengthList):
    print(list[i])
list = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee", "Zulu"]

# Once again, taking the length of the list
lengthList = len(list)

# Setting the variable we are going to use as 0
i=0 

# Iteration using the while loop 
# Argument saying WHILE a certain variable is a certain condition, the code should run
while i < lengthList:
    print(list[i])
    i += 1

Using the range() function

Need to save even more time? The above is useful for many occasions, but can get tedious fast, in this case, use range()

x = range(5)

for n in x:
    print(n)

Else, elif, and break

Else:when the condition does not meet, do statement()- Elif: when the condition does not meet, but meets another condition, do statement() Break: stop the loop

HW Iteration

Use the list below to turn the first letter of any word (using input()) into its respective NATO phonetic alphabet word

Ex:

list ->

lima india sierra tango

words = ["alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliett", "kilo",
"lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "xray", "yankee", "zulu"]

inp = input().lower()

print(inp)

for i in inp:
    for n in words:
        if i == n[0]:
            print(n)
luke
lima
uniform
kilo
echo

2D Arrays

A 2D array is simply just a list of lists. The example below is technically correct but...

keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [" ", 0, " "]]

Conventially 2D arrays are written like below. This is because 2D arrays are meant to be read in 2 dimensions (hence the name). Writing them like below makes them easier to visualize and understand.

keypad =   [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [" ", 0, " "]]

Printing a 2D Array

We already know that we can't just print the matrix by calling it. We need to iterate through it to print it.

matrix = ["dog", "cat", "bird"]

def print_matrix1(matrix): 
    for i in range(len(matrix)):  # outer for loop. This runs on i which represents the row. range(len(matrix)) is in order to iterate through the length of the matrix
        for j in range(len(matrix[i])):  # inner for loop. This runs on the length of the i'th row in the matrix (j changes for each row with a different length)
            print(matrix[i][j], end=" ")  # [i][j] is the 2D location of that value in the matrix, kinda like a coordinate pair. [i] iterates to the specific row and [j] iterates to the specific value in the row. end=" " changes the end value to space, not a new line.
        print() # prints extra line. this is in the outer loop, not the inner loop, because it only wants to print a new line for each row
matrix = ["dog", "cat"]

def print_matrix2(matrix):
    for row in matrix:  # Iterates through each "row" of matrix. Row is a dummy variable, it could technically be anything. It iterates through each value of matrix and each value is it's own list. in this syntax the list is stored in "row".
        for col in row:  # Iterates through each value in row. Again col, column, is a dummy variable. Each value in row is stored in col.
            print(col, end=" ") # Same as 1
        print() # Same as 1

print_matrix2(matrix)
d o g 
c a t 

More Functions

Try to find another way to print the matrix. Only complete one of the two (unless you'd like to do both). Below is a hint

fruit = ["apples", "bananas", "grapes"]
print(fruit)
print(*fruit) # Python built in function: "*". Figure out what it does
['apples', 'bananas', 'grapes']
apples bananas grapes

Different way to print the matrix

keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [" ", 0, " "]]


def print_matrix3(matrix):
    for i in matrix:
        print(*i)
        
print_matrix3(keypad)
1 2 3
4 5 6
7 8 9
  0  

HW

matrix = [["`", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "-", "="],
            ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]"],
            ["A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'"],
            ["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]

#def print_matrix1(matrix): 
#    for i in range(len(matrix)):  # outer for loop. This runs on i which represents the row. range(len(matrix)) is in order to iterate through the length of the matrix
#        for j in range(len(matrix[i])):  # inner for loop. This runs on the length of the i'th row in the matrix (j changes for each row with a different length)
#            print(matrix[i][j], end=" ")  # [i][j] is the 2D location of that value in the matrix, kinda like a coordinate pair. [i] iterates to the specific row and [j] iterates to the specific value in the row. end=" " changes the end value to space, not a new line.
#        print() # prints extra line. this is in the outer loop, not the inner loop, because it only wants to print a new line for each row
#

def print_matrix2(matrix):
    for row in matrix:  # Iterates through each "row" of matrix. Row is a dummy variable, it could technically be anything. It iterates through each value of matrix and each value is it's own list. in this syntax the list is stored in "row".
        for col in row:  # Iterates through each value in row. Again col, column, is a dummy variable. Each value in row is stored in col.
            print(col, end=" ") # Same as 1
        print() # Same as 1

print(matrix[2][8] + matrix[1][6] + matrix[2][7] + matrix[1][2])
print(matrix[2][6] + matrix[1][6] + matrix[2][8] + matrix[1][5])
print(matrix[0][9] + matrix[0][9])
LUKE
JULY
18