Jed Rembold
October 17, 2025
len function+ or
+=*in operatorWhat would the below expression evaluate to?
['One', 2, True][-1:1:-1][0]
['One']2Trueid(|||object|||)
function>>> A = 5
>>> id(A)
129257497672776
>>> B = 5
>>> id(B)
129257497672776
>>> C = [5]
>>> id(C)
129257425943936
>>> D = [5]
>>> id(D)
129257426156544
== compares the contents of
listsis compares the ids of
listscool = ['blue', 'violet']
warm = ['red', 'orange']
colors = [cool, warm]
other_colors = [['blue', 'violet'],
['red', 'orange']]
print(colors == other_colors)
print(colors is other_colors)
cool[0] = 'indigo'
warm = ['orange', 'yellow']
print(colors)
print(other_colors)
.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)
Commonly will make lists with a simple:
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 select specific
valuesExample: all even numbers to 20 not also visible by 3
[i for i in range(0,2,20) if i % 3 != 0]| Method | Description |
|---|---|
str.split() |
Splits a string into a list of its components using whitespace as a separator |
str.split(sep) |
Sprits a string into a list using the specified separator
sep |
str.splitlines() |
Splits a string into of list of strings at the newline character |
str.join(list) |
Joins the elements of the list into a
string, using str as the separator |