Data Types - List Flashcards

1
Q

What is the fastest and most memory-efficient way to generate a list of numbers

A

list(range(n))

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

fruits = [“apple”, “banana”, “cherry”]

Give 3 ways to remove “bananna” from this list

A

del fruits[1]

fruits.pop(1)

fruits.remove(“banana”)

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

a = [ ]
a += 15

What does the above return

A

TypeError

+= can be used to extend a String, List or Tuple, but 15 is an integer.

Further, for List only, .extend() gives the same result ( .extend() is specific to Lists)

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

a = [ ]

Give the best 2 ways to add the integer 15 to list “a” as an item

A

a.append(15)

a += [15]

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

for item in lst:
print(lst[item])

What does the above print

A

TypeError

This treats “item” like it’s an index, but it’s an item (in this case an item in “list”).

If you want to print the Item, it should be typed as below,

for item in list:
print(item)

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

lst = [7,2,9,4,7,1,8,5,3]

Find the total of these numbers

A

sum(lst)

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

lst = [1,2,3,4,5]

Print “5” from “lst”

A

print(lst[-1])

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

lst = [1,2,3,4,5]

Print “1” from “lst”

A

print(lst[0])

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

lst = [1,2,3,4,5]

what does each of the lines below print

lst[0]

lst[0:]

A

lst[0] = 0

lst[0:] = [1,2,3,4,5]

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

lst = [1,2,3,4,5]

what does each of the lines below print

lst[-1]

lst[-1:]

A

lst[-1] = 5

lst[-1:] = [5]

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