Given the 2D list below, print every element at an even index of each inner list. Consider 0 to be even. Hint: A special message should appear if you did it right!
my_list = [
["awesome", "hello", "job", "world"],
["you", "words", "got", "books"],
["it", "python", "right"],
["keep", "plant", "learning"],
["how", "school", "to"],
["code"],
]
💻 Template code
✅ Solution code
Here is the challenge problem for 2D loops:
Images are often represented as 3D arrays, where the rows and columns are the pixels in the image, and each pixel has an r, g, and b value
The interesting thing is that we can iterate over images. The challenge is, given an image, create a program that will return a different image where each pixel is the average of the pixels surrounding it in the original image
The neighbors of an image are all the pixels that surround it, 1 on each of its 4 adjacent sides, and 4 on the diagonals, for 8 in total.
Not every pixel has 8 neighbors though (think about why).
The code to grab an image from the internet and make it into an array is given to you. The code also displays the new image you create in the end.
<aside> 💡 The image is 3D because each pixel has rgb values. To find the average value of all of a pixel's neighbors, you must change the average of the red value to the red value, blue to blue, etc. For example, if the neighbors ofa pixel with value [1, 2, 3] were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the original one would be [15, 75, 35]
</aside>
from PIL import Image
import requests
import numpy
import matplotlib.pyplot as plt
url = "<https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg>"
img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist()
newimg = img
transpose = numpy.transpose(img)
plt.imshow(img)
plt.show()
# write code to create newimg here
plt.imshow(newimg)
plt.show()
plt.imshow(transpose)
plt.show()
💻 Template code
✅ Solution code
Create a 2D list where each row represents a person and has 3 elements: their name, their age, and their address.
The entire list should have 4 such entries (4 people), so it will be a 4x3 list. Display the name and address of the 2nd person in the list. Then, display the entire list with the format: name (age): address