list
Functionfor
Loop with Listslen
Functionin
Operatordel
StatementYou should read Chapter 8, More About Strings, from our textbook, Starting Out with Python, before next week's class.
I have posted homework 11 here.
This is the last homework assignment.
The answers to Quiz 8 are posted here.
Let's take a look.
>>> numbers = [5, 8, 2, 9, 7, 6, 3] >>> numbers [5, 8, 2, 9, 7, 6, 3]
list
Functionlist
is a conversion function just like int
and str
list
turns objects that are collections of things into a listlist
will work on strings
>>> name = "Glenn" >>> characters = list(name) >>> characters ['G', 'l', 'e', 'n', 'n']
>>> name = "Glenn" + " " + "Hoffman" >>> name 'Glenn Hoffman'
>>> n1 = [1,2,3] >>> n2 = [4,5,6] >>> n1 + n2 [1, 2, 3, 4, 5, 6]
>>> n1 / n2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'list'
>>> zeros = [0] * 5 >>> zeros [0, 0, 0, 0, 0] >>> numbers = [1, 2, 3] * 3 >>> numbers [1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> "Go " * 3 'Go Go Go '
for
Loop with Listsfor
loop will work with a list
>>> numbers = [1, 2, 3, 4, 5] >>>for n in numbers: ... print(n) ... 1 2 3 4 5
for
loop with strings
>>> team = "Red Sox" >>> for char in team: ... print(char) ... R e d S o x >>>
>>> even_numbers[0] 2 >>> even_numbers[1] 4 >>> even_numbers[2] 6
len
Functionlen
will return the length of any sequence
>>> even_numbers = [2, 4, 6, 8, 10] >>> len(even_numbers) 5 >>> len("foo")
len
with range
to iterate through a list
for i in range(len(even_numbers)): ... print(even_numbers[i]) ... 2 4 6 8 10
numbers = [1, 4, 6, 8]
>>> numbers[0] = 2 >>> numbers [2, 4, 6, 8]
for
loop to change every element inside a list
>>> for i in range(len(numbers)): ... numbers[i] += 1 ... >>> numbers [3, 5, 7, 9]
>>> new_list = [] >>> new_list []
>>> new_list * 5 []
>>> days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX]
>>> weekdays = days[1:6] >>> weekdays ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
digits[2:8] [3, 4, 5, 6, 7, 8]
>>> days ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> days[:4] ['Sunday', 'Monday', 'Tuesday', 'Wednesday'] >>> digits [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> digits[:5] [1, 2, 3, 4, 5]
>>> days[4:] ['Thursday', 'Friday', 'Saturday'] >>> digits[5:] [6, 7, 8, 9, 0]
>>> days[:] ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> digits[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> daynames = days[:] >>> daynames ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
range
function
LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX:STEP_VALUE]
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> digits[1:10:2] [2, 4, 6, 8, 0]
digits[1::2] [2, 4, 6, 8, 0]
in
Operatorin
is an operator that works on objects
that are a collection of values
VALUE in LIST
in
operator returns True
if the LIST
contains VALUE
False
>>> digits [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> 1 in digits True >>> 11 in digits False
>>> l = [1, 2, 3, 3, 3, 4, 5, 6]
in
will tell you that value is in a list
>>> 3 in l True
Method | Description |
---|---|
append(item) | Adds item to the end of the list |
pop(index) | Removes the element at a given index from the list and returns the value. If called with no argument, it returns the last element and deletes it from the list. |
index(item) | Returns the index of the first element whose value is equal to item. A ValueError exception is raised if item is not found in the list. |
insert(index, item) | Inserts item into the list at the specified index. When an item is inserted into a list, the list is expanded in size to accommodate the new item. The item that was previously at the specified index, and all the items after it, are shifted by one position toward the end of the list. |
sort() | Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value) |
reverse() | Reverses the order of the items in the list |
>>> teams = [] >>> teams []
>>> teams.append('Red Sox') >>> teams ['Red Sox']
>>> teams.append('Orioles') >>> teams.append('Blue Jays') >>> teams.append('Rays') >>> teams.append('Yankees')
>>> teams ['Red Sox', 'Orioles', 'Blue Jays', 'Rays', 'Yankees']
>>> numbs = [1, 2, 3, 4, 5] >>> n = numbs.pop(0) >>> n 1 >>> numbs [2, 3, 4, 5]
>>> n = numbs.pop() >>> n 5 >>> numbs [2, 3, 4]
in
operator will tell us if a list contains a value ...>>> 'Red Sox' in teams True
>>> teams.index('Red Sox') 0 >>> teams.index('Blue Jays') 2
teams.index('Mets')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'Mets' is not in list
>>> >>> l = [1, 2, 3, 3, 5] >>> l.index(3) 2
>>> l1 = [1, 2, 3, 4, 5] >>> l1.insert(2, 7) >>> l1 [1, 2, 7, 3, 4, 5]
>>> l2 = [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>> l2 [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>>l2.sort() >>> l2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l3 = [5, 4, 3, 2, 1] >>> l3.reverse() >>> l3 [1, 2, 3, 4, 5] >>> l3.reverse() >>> l3 [5, 4, 3, 2, 1]
>>> l4 = [2, 5, 9, 1, 8, 6, 3, 7, 4] >>> l4.sort() >>> l4 [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l4.reverse() >>> l4 [9, 8, 7, 6, 5, 4, 3, 2, 1]
del
Statementdel
statement
del LIST_VARIABLE[INDEX]
del
comes the list variable and the index inside square brackets
>>> l5 = [1, 2, 3, 4, 5, 6] >>> del l5[2] >>> l5 [1, 2, 4, 5, 6]
min
returns the smallest value in a list
>>> l6 [1, 4, 5, 6] >>> min(l6) 1
max
returns the largest
max(l6) 6
Be honest and trustworthy
Be fair and take action not to discriminate