Fred Agbo
October 19, 2026
4BenGT
| 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 orderdel statementlist1 =[2,4,6,5,3]
del list1[3]
print(list1)
list1 = [2,4,6,5,3]
list2 =[] #to store ele
for i in range(len(list1)):
if list1[i]%2==0: #remove odd
list2.append(list1[i])
print(list1)
print(list2)
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,20,2) if i % 3 != 0]Given the code to the right, what would be the printed value of
A?
['Fox', 'Giraffe', 'Hippo', 'Iguana']['Fox', 'Hippo', 'Iguana']['Iguana', 'Fox']A = [
'Fox',
'Giraffe',
'Hippo'
]
A.append('Iguana')
A[:].reverse()
B = A
for anim in B:
if anim[1] == 'i':
B.remove(anim)
print(A)
raw_data.py contains the
constant DATA, which is a large list of
stringsappend to create a list of only the entries
that are numbersid before your code and after you code is
still the same!