chapter 9 practice questions: Lists Flashcards
What will the following print?
cheeses = [‘Cheddar’, ‘Edam’, ‘Gouda’]
print(‘Edam’ in cheeses)
print(‘Brie’ in cheeses)
- True
- False
What is printed by the following?
alist = [4, 2, 8, 6, 5]
alist[2] = True
print(alist)
[4, 2, True, 6, 5]
What is printed by the following?
alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False]
print(57 in alist)
False
What will happen if you attempt to traverse an empty list?
Nothing will happen
How many items are in this list?
nestedList = [[“First”, 2, [“Third”]]]
1
What will the following print?
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
[1, 2, 3, 4, 5, 6]
What does the following print?
print([0] * 4)
print([1, 2, 3] * 3)
[0, 0, 0, 0]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
What will be printed by the following?
alist = [4, 2, 8, 6, 5]
alist = alist + 999
print(alist)
Error, you cannot concatentate a list with an integer
What does the following print?
t = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
t[1:3] = [‘x’, ‘y’]
print(t)
[‘a’, ‘x’, ‘y’, ‘d’, ‘e’, ‘f’]
What is printed by the following?
alist = [4, 2, 8, 6, 5]
alist.append(True)
alist.append(False)
print(alist)
[4, 2, 8, 6, 5, True, False]
what does .extend() do?
takes a list as an argument and appends all of the elements
What does the following print?
t1 = [‘a’, ‘b’, ‘c’]
t2 = [‘d’, ‘e’]
t1.extend(t2)
print(t1)
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
what does .sort() do?
arranges the elements of the list from low to high
True or False? The sort method alphabetizes lists
False
what does .pop() do?
modifies the list and returns the element that was removed
What happens when you don’t provide an index for .pop()?
If you don’t provide an index, it deletes and returns the last element.
What is printed by the following?
alist = [4, 2, 8, 6, 5]
temp = alist.pop(2)
temp = alist.pop()
print(alist)
[4, 2, 6]
What is the difference between del and .remove() ?
both are for removing an item in a list, but .remove() is used when you don’t know the index for an item
What is the return value of .remove() ?
None
Contrast sum() with len() and max()
sum() can only work when all the items in the string are numbers
len() and max() can work with other types that are comparable
What does the following print?
s = ‘spam’
t = list(s)
print(t)
[’s’, ‘p’, ‘a’, ‘m’]
what is .split()?
function to break a str into letters
the delimiter is where you want to split it
ex. .split(delimiter)
What is printed by the following?
myname = “Edgar Allan Poe”
namelist = myname.split()
init = “”
for aname in namelist:
init = init + aname[0]
print(init)
what is .join()?
opposite of .split(); concatenates the strs in a list
also uses delimter