2018-03-08 HW

posted Mar 8, 2018, 6:19 AM by Konstantinovich Samuel   [ updated Mar 8, 2018, 11:37 AM ]

More on Lists

Homework - Codingbat list problems 3 more were added ( codingbat.com/home/konstans@stuy.edu/intro )

1. List elements can be changed with assignment:

x = [ 3, 4, 5]
x[0] = 99 #replace 3 with 99
print x   #shows [99,4,5]

2. List slices can be replaced too!
x = [ 3, 4, 5, 6, 7]
x [ 1 : 3] = [0,0,0]  #replace 4,5 with 0,0,0
print x               #shows [3,0,0,0,6,7]

Do now:
Write a function that accepts two lists as parameters and returns a new list.
def zip( a, b ):

The returned list should have the elements in alternating order that is:
a[0], then b[0], then a[1], then b[1] ... until you run out of elements.
When one list runs out of elements, the remaining elements are put at the end.
zip( [1,2,3] , ['a','b'] )# [1,'a',2,'b',3]
zip( [1,2] , [5,5,5,5] )  # [1,5,2,5,5,5]
zip( [1] , [] )           # [1]


Lets write a few simple list functions:

def mySum(listOfNumbers):

def myMax(listOfNumbers):

Write a function that accepts a list as a parameter and returns a copy of a list that is in the reverse order:
#do not use reverse, do not use [::-1]

def myReverse( stuff ):

print myReverse([1,2,3] )  #shows [3,2,1]
print myReverse([] )       #shows []

The list data type has some more methods. Here are all of the methods of list objects:


Add an item to the end of the list. This is equivalent to a[len(a):] = [x] and not something I would have you write, it is a basic feature of lists.

list.extend(iterable)  (an iterable is something with many parts like a list or string)

Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.


Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).


Remove the first item from the list whose value is x. It is an error if there is no such item.


Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)


Return zero-based index in the list of the first item whose value is x. Raises a ValueError if there is no such item.

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.


Return the number of times x appears in the list.


Reverse the elements of the list in place.

An example that uses most of the list methods:

>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
>>> fruits.count('tangerine')
>>> fruits.index('banana')
>>> fruits.index('banana', 4)  # Find next banana starting a position 4
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()