Python: Lists Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Which of the following is the correct way to turn the following into a list of names: “Tom”, “Jerry”, “Tweetie”, “Sylvester”?

names = [“Tom”, “Jerry”, “Tweetie”, “Sylvester”]

names = “Tom”, “Jerry”, “Tweetie”, “Sylvester”

names = [“Tom” “Jerry” “Tweetie” “Sylvester”]

names = {“Tom”, “Jerry”, ‘Tweetie”, “Sylvester”}

A

names = [“Tom”, “Jerry”, “Tweetie”, “Sylvester”]

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

Which of the following is the correct way to remove the first instance of “Rio” from name_list?

name_list.remove([Rio])

remove(name_list[“Rio”])

“Rio”.remove(name_list)

name_list.remove(“Rio”)

A

name_list.remove(“Rio”)

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

Is the following list a valid Python list?

mylist = [“Mount Everest”, 29029]

A

Yes, lists can contain multiple data types.

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

Which of the following is the correct way to create an empty list?

empty_list = []

empty_list = [0]

empty_list = {}

empty_list = None

A

empty_list = []

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

What would be the proper way to access “Strawberry” from the list groceries

groceries = [“Grapes”, “Strawberry”, “Starfruit”, “Apple”]

A

groceries[1]

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

The Python method .remove() will delete every instance of a provided value.

A

False

👏
Correct! The .remove() method removes only the first matching element in a list.

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

Which of the following is the correct way to add the number 4 to number_list?

number_list.append([4])

number_list.append(4)

4.append(number_list)

number_list + 4

A

number_list.append(4)

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

How would you access “77.45” from the following list?

student_data = [[“Ali”, 90], [“Bob”, 87.5], [“Cam”, 80.3], [“Doug”, 77.45]]

A

student_data[-1][-1]

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

Which of the following lines of code will correctly sort mylist?

mylist.sorted()

sort(mylist)

mylist.sort

mylist.sort()

A

mylist.sort()

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

Which of the following lines of code would slice the list [“b”, “c”] from mylist:

mylist = [“a”, “b”, “c”, “d”, “e”]

A

mylist[1:3]

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

Which list would be created by running this code?

list(range(2, 14, 4))

A

[2, 6, 10]

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

What would create a range object that starts at 3 and goes up to 15 (non-inclusive) in increments of 4?

A

range(3, 15, 4)

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

Which of the following lines of code will tell us how many times “win” appears in the list game_results?

count(game_results, “win”)

“win”.count(game_results)

game_results.count(“win”)

A

game_results.count(“win”)

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

What would be generated by the code snippet?

mylist = [“a”, “b”, “c”, “d”, “e”, “f”]
print(mylist[2:5])

A

[“c”, “d”, “e”]

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

What will return the length of the list mylist?

A

len(mylist)

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

Which of the following lines of code would remove “Breakdancing” from the list olympic_sports.

olympic_sports = [“Hockey”, “Swimming”, “Fencing”, “Volleyball”, “Breakdancing”]

olympic_sports.pop(-2)

olympic_sports.pop()

olympic_sports.pop(3)

olympic_sports.pop[“Breakdancing”]

A

olympic_sports.pop()

17
Q

Which of the following lines of code will insert “Gus” as the middle element of the list friends?

friends = [“Annabelle”, “Greg”, “Katya”, “Sol”]

friends.insert(-2, “Gus”)

insert(friends, 2, “Gus”)

friends.insert(-3, “Gus”)

friends.insert(1, “Gus”)

A

friends.insert(-2, “Gus”)

18
Q

Which of the following code selects the last three elements of mylist?

mylist[-3:]

mylist[:-3]

mylist[-3, -2, -1]

mylist[3:]

A

mylist[-3:]