PCEP 3.6 Flashcards
Slices and manipulation of lists
What is a slice
An element of Python syntax that allows you to make a brand new copy of a list, or parts of a list
What does this do …
list_1[0] = 2
It replaces the first position in a list with the integer ‘2’
what is the syntax of a slice?
my_list[start:end]
what is the output of…
my_list = [10, 8, 6, 4, 2]
new_list = my_list[:3]
print(new_list)
[10, 8, 6]
what is a more compact version of…
my_list[start:len(my_list)]
my_list[:]
remove a slice from the list …
list = [1, 2, 3, 4, 5]
del list[2:3]
what is the output of…
list = [1, 2, 3, 4, 5]
del list[2:3]
print(list)
[1, 2, 4, 5]
what does this do? …
del my_list
deletes the whole list, not just it’s elements
what is the output of …
my_list = [0, 3, 12, 8, 2]
print(5 in my_list)
print(5 not in my_list)
print(12 in my_list)
False
True
True
Write a program that finds the largest valur element in a list
my_list = [17, 3, 11, 5, 100, 9, 7, 15, 13]
largest = my_list[0]
for i in range(1, len(my_list)):
if my_list[i] > largest:
largest = my_list[i]
print(largest)
What is the output of the following snippet?…
list_1 = [“A”, “B”, “C”]
list_2 = list_1
list_3 = list_2
del list_1[0]
del list_2[0]
print(list_3)
[“C”]
What is the output? …
list_1 = [“A”, “B”, “C”]
list_2 = list_1
list_3 = list_2
del list_1[0]
del list_2
print(list_3)
[‘B’, ‘C’]
What is the output? …
list_1 = [“A”, “B”, “C”]
list_2 = list_1
list_3 = list_2
del list_1[0]
del list_2[:]
print(list_3)
[]
what is the output? …
list_1 = [“A”, “B”, “C”]
list_2 = list_1[:]
list_3 = list_2[:]
del list_1[0]
del list_2[0]
print(list_3)
[‘A’, ‘B’, ‘C’]
nsert in or not in instead of ??? so that the code outputs the expected result.
my_list = [1, 2, “in”, True, “ABC”]
print(1 ??? my_list) # outputs True
print(“A” ??? my_list) # outputs True
print(3 ??? my_list) # outputs True
print(False ??? my_list) # outputs False
in
not in
not in
in