Data Types - List Flashcards
What is the fastest and most memory-efficient way to generate a list of numbers
list(range(n))
fruits = [“apple”, “banana”, “cherry”]
Give 3 ways to remove “bananna” from this list
del fruits[1]
fruits.pop(1)
fruits.remove(“banana”)
a = [ ]
a += 15
What does the above return
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)
a = [ ]
Give the best 2 ways to add the integer 15 to list “a” as an item
a.append(15)
a += [15]
for item in list:
print(list[item])
What does the above print
TypeError
This treats “item” like it’s an index, but it’s an item (in this case a item in “list”).
If you want to print the Item, it should be typed as below,
for item in list:
print(item)
lst = [7,2,9,4,7,1,8,5,3]
Find the total of these numbers
sum(lst)
lst = [1,2,3,4,5]
Print “5” from “lst”
print(lst[-1])
lst = [1,2,3,4,5]
Print “1” from “lst”
print(lst[0])
lst = [1,2,3,4,5]
what does each of the lines below print
lst[0]
lst[0:]
lst[0] = 0
lst[0:] = [1,2,3,4,5]
lst = [1,2,3,4,5]
what does each of the lines below print
lst[-1]
lst[-1:]
lst[-1] = 5
lst[-1:] = [5]