Jed Rembold
October 20, 2025
.copy() list methodlist() function will return a
new object| Method | Description |
|---|---|
|||list|||.copy() |
Returns a new list whose elements are the same as the original |
|||list|||.append(|||value|||) |
Adds |||value||| to the end of the
list |
|||list|||.insert(|||idx|||, |||val|||) |
Inserts |||val||| before the specified
|||idx||| |
|||list|||.remove(|||value|||) |
Removes the first instance of
|||value||| from the list, or errors |
|||list|||.reverse() |
Reverses the order of the elements in the list |
|||list|||.sort() |
Sorts the elements of the list. Can take an optional argument
|||key||| to specify how to sort |
.sort and
.reverse methods reorder the list in
place and do not return anythingreversed() function creates a new
iterable object that returns its elements in the opposite ordersorted() function creates a new
iterable object that returns its elements in ascending orderGiven the code to the right, what would be the printed value of
A?
['Fox', 'Giraffe', 'Hippo', 'Iguana']['Fox', 'Hippo', 'Iguana']['Iguana', 'Fox']['Fox', 'Iguana']A = [
'Fox',
'Giraffe',
'Hippo'
]
A.append('Iguana')
A[:].reverse()
B = A
for anim in B:
if anim[1] == 'i':
B.remove(anim)
print(A)
For simple lists, we will commonly explicitly write out the elements:
even_digits = [ 2, 4, 6, 8 ]But in many cases, it is easier to specify the elements of a list
using a sequence of values generated by a
for loop. For instance
even_digits = [ ]
for i in range(0, 10, 2):
even_digits.append(i)Python gives us a shorthand notation to achieve this:
even_digits = [ i for i in range(0, 10, 2) ]
The simplest list comprehension syntax is:
[ |||expression||| |||iterator||| ]
where |||expression||| is any Python
expression and |||iterator||| is a
for loop header
The iterator component can be followed by any number of additional modifiers
for loop headers for nested
loopsif statements to only include
specific valuesExample: all even numbers to 20 not also visible by 3
[i for i in range(0,2,20) if i % 3 != 0]List of valid 5 letter words
from english import ENGLISH_WORDS
a = [word for word in ENGLISH_WORDS if len(word) == 5]List of first 20 positive squares
a = [i ** 2 for i in range(1, 21)]List of non-alphabetical characters in string
s = "Jack & Jill's Story Time!"
a = [char for char in s if not char.isalpha()]We know that elements of a list can be lists in and of themselves. If the lengths of all the lists making up the elements of a list remain fixed, then the list of lists is called a multidimensional array
In Python, we can create multidimensional arrays just by creating lists of constant length as the elements to another list
magic = [ [2, 9, 4], [7, 5, 3], [6, 1, 8] ]We can always get the individual element of one of the inner lists by using two indices.
magic[1][1] = 5magic[-1][0] = 6[ [2, 9, 4], [7, 5, 3], [6, 1, 8] ]
GImage ClassGImage class.
GImage(|||filename|||, |||x|||, |||y|||)
|||filename||| is the string containing
the name of the file which contains the image|||x||| and
|||y||| are the coordinates of the upper
left corner of the imagefish.giffish.jpgfish.pngwww.nasa.gov can be
freely used as long as you add an attribution linefrom pgl import GImage, GWindow, GLabel
def image_example():
gw = GWindow(800, 550)
image = GImage("VLA_Moonset.jpg")
image.scale(gw.get_width() / image.get_width())
gw.add(image)
citation = GLabel("Image Credit: Jeff Hellermann, NRAO / AUI / NSF")
citation.set_font("15px 'Sans-Serif'")
x = gw.get_width() - citation.get_width() - 10
y = image.get_height() + citation.get_ascent()
gw.add(citation, x, y)
Image data is commonly stored in two-dimensional arrays
Each element stores information about the pixel that exists at that location
The GImage class lets you convert
between the image itself and the array representing the image contents
by using the get_pixel_array method, which
returns a two-dimensional array of integers.
We could get the pixels from our example image using:
image = GImage("VLA_Moonset.jpg")
pixels = image.get_pixel_array()The first index of the pixel array gets you the row, the second index gets you the column
10010101 →
0x9500111001 →
0x3901100011 →
0x63#953963 or
| Function | Description |
|---|---|
GImage.get_red(|||pixel|||) |
Returns the integer (0-255) corresponding to the red portion of the pixel |
GImage.get_green(|||pixel|||) |
Returns the integer (0-255) corresponding to the green portion of the pixel |
GImage.get_blue(|||pixel|||) |
Returns the integer (0-255) corresponding to the blue portion of the pixel |
GImage.get_alpha(|||pixel|||) |
Returns the integer (0-255) corresponding to the alpha portion of the pixel |
GImage.create_rgb_pixel(|||r|||,|||g|||,|||b|||) |
Returns a 32-bit integer corresponding to the desired color |