Nested loops & 2-D lists · Bucles anidados y listas 2-D
A loop inside a loop
- You can put one loop inside another loop.
- For every pass of the outer loop, the inner loop runs fully.
- So an outer of 2 and an inner of 3 runs the body 2 × 3 = 6 times.
Un bucle dentro de otro
- Puedes colocar un bucle dentro de otro bucle.
- Por cada paso del bucle externo, el bucle interno se ejecuta completamente.
- Por lo tanto, un externo de 2 y un interno de 3 ejecutan el cuerpo 2 × 3 = 6 veces.
for row in range(2):
for col in range(3):
print(row, col)
A list of lists
- A 2-D list is a list whose items are themselves lists.
- It models a grid or table: rows of columns.
- Reach one cell with two indexes:
grid[row][col].
Una lista de listas
- Una lista 2-D es una lista cuyos elementos son a su vez listas.
- Modela una cuadrícula o tabla: filas de columnas.
- Accede a una celda con dos índices:
grid[row][col].
grid = [[1, 2], [3, 4]]
print(grid[0][1])
print(grid[1][0])
Loop over a grid
- Use a nested loop to visit every cell.
- The outer loop walks the rows; the inner loop walks the columns.
for row in grid:gives one row (a list) at a time.
Recorrer una cuadrícula
- Usa un bucle anidado para visitar cada celda.
- El bucle externo recorre las filas; el bucle interno recorre las columnas.
for row in grid:obtiene una fila (una lista) a la vez.
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
In Cambridge pseudocode
- A 2-D list is the exam's 2-D array, written
ARRAY[1:rows, 1:cols].
En pseudocódigo de Cambridge
- Una lista 2-D corresponde al array 2-D del examen, escrito como
ARRAY[1:rows, 1:cols].
DECLARE grid : ARRAY[1:2, 1:3] OF INTEGER
FOR r ← 1 TO 2
FOR c ← 1 TO 3
OUTPUT grid[r, c]
NEXT c
NEXT r
Common mistakes
- The inner loop finishes all its turns for each single turn of the outer loop.
- Index a grid row first, then column:
grid[row][col].
Errores comunes
- El bucle interno termina todas sus iteraciones por cada única iteración del bucle externo.
- Indexa primero la fila de la cuadrícula y luego la columna:
grid[row][col].
Now you try
- Use a nested loop in each task.
- Press Check answer to test your code.
Ahora tú prueban
- Usa un bucle anidado en cada tarea.
- Pulsa Check answer para probar tu código.
A 2-D list is a grid · Una lista 2-D es una cuadrícula
A 2-D list is rows of columns; reach a cell with grid[row][col]. · Una lista 2-D son filas de columnas; accede a una celda con grid[row][col].
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters. · Usa un bucle anidado para imprimir un cuadrado de asteriscos de 3x3: tres líneas, cada una con tres caracteres *.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21). · Suma todos los números en la lista 2-D grid usando un bucle anidado. Almacena el total en total (la respuesta es 21).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Count how many cells in the 2-D list grid are equal to 0. Store the count in count (the answer is 3). · Cuenta cuántas celdas en la lista 2-D grid son iguales a 0. Almacena el conteo en count (la respuesta es 3).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.