chapter 9 practice questions: Lists Flashcards

1
Q

What will the following print?

cheeses = [‘Cheddar’, ‘Edam’, ‘Gouda’]

print(‘Edam’ in cheeses)
print(‘Brie’ in cheeses)

A
  1. True
  2. False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is printed by the following?

alist = [4, 2, 8, 6, 5]
alist[2] = True

print(alist)

A

[4, 2, True, 6, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is printed by the following?

alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False]

print(57 in alist)

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What will happen if you attempt to traverse an empty list?

A

Nothing will happen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many items are in this list?

nestedList = [[“First”, 2, [“Third”]]]

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will the following print?

a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)

A

[1, 2, 3, 4, 5, 6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the following print?

print([0] * 4)
print([1, 2, 3] * 3)

A

[0, 0, 0, 0]
[1, 2, 3, 1, 2, 3, 1, 2, 3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will be printed by the following?

alist = [4, 2, 8, 6, 5]
alist = alist + 999
print(alist)

A

Error, you cannot concatentate a list with an integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the following print?

t = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
t[1:3] = [‘x’, ‘y’]
print(t)

A

[‘a’, ‘x’, ‘y’, ‘d’, ‘e’, ‘f’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is printed by the following?
alist = [4, 2, 8, 6, 5]
alist.append(True)
alist.append(False)
print(alist)

A

[4, 2, 8, 6, 5, True, False]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what does .extend() do?

A

takes a list as an argument and appends all of the elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the following print?

t1 = [‘a’, ‘b’, ‘c’]
t2 = [‘d’, ‘e’]
t1.extend(t2)
print(t1)

A

[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does .sort() do?

A

arranges the elements of the list from low to high

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False? The sort method alphabetizes lists

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does .pop() do?

A

modifies the list and returns the element that was removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens when you don’t provide an index for .pop()?

A

If you don’t provide an index, it deletes and returns the last element.

17
Q

What is printed by the following?
alist = [4, 2, 8, 6, 5]
temp = alist.pop(2)
temp = alist.pop()
print(alist)

A

[4, 2, 6]

18
Q

What is the difference between del and .remove() ?

A

both are for removing an item in a list, but .remove() is used when you don’t know the index for an item

19
Q

What is the return value of .remove() ?

A

None

20
Q

Contrast sum() with len() and max()

A

sum() can only work when all the items in the string are numbers
len() and max() can work with other types that are comparable

21
Q

What does the following print?

s = ‘spam’
t = list(s)
print(t)

A

[’s’, ‘p’, ‘a’, ‘m’]

22
Q

what is .split()?

A

function to break a str into letters
the delimiter is where you want to split it
ex. .split(delimiter)

23
Q

What is printed by the following?

myname = “Edgar Allan Poe”
namelist = myname.split()
init = “”
for aname in namelist:
init = init + aname[0]
print(init)

A
24
Q

what is .join()?

A

opposite of .split(); concatenates the strs in a list
also uses delimter

25
Q

how to concatenate strings without spaces?

A

use empty str, ‘’ as delimiter in .join()

26
Q

What does the following print?

mylist = [‘Hannah’, ‘Grace’, ‘Olivia’, ‘Ruth’]
delimiter = ‘’
print(delimiter.join(mylist)

A

HannahGraceOliviaRuth

27
Q

When reading lines from a file, what list method can be used to select or print parts of a line?

A

split

28
Q

what is the ‘is’ operator used for?

A

To check whether two variables refer to the same object
ex.
a = ‘banana’
b = ‘banana’
print(a is b)

29
Q

What will the following print? Why?

a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)

A

False; 2 lists are 2 different objects

30
Q

When two lists have the same elements, but are different objects we say they are __________?

A

equivalent

31
Q

Equivalent or identical?

a = [1, 2, 3] b = a

A

identical

32
Q

An object with more than one reference has more than one name, so we say that the object is ______?

A

aliased, they are mutable

33
Q

What is printed by the following?

alist = [4, 2, 8, 6, 5]
blist = alist
blist[3] = 999
print(alist)

A

[4, 2, 8, 999, 5]

34
Q

What does the following print?
def myfun(lst):
del lst[0]

mylist = [‘a’, ‘b’]
myfun(mylist)
print(mylist)

A

[‘b’]

35
Q

What is the difference between .append() and +?

A

.append() modifies the existing list, + creates a new object

36
Q

Does this function remove the head of the list? what does it do?

def bad_delete_head(t):
t = t[1:]

A

The slice operator creates a new list and the assignment makes t refer to it, but none of that has any effect on the list that was passed as an argument