PCEP 3.6 Flashcards

Slices and manipulation of lists

1
Q

What is a slice

A

An element of Python syntax that allows you to make a brand new copy of a list, or parts of a list

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

What does this do …

list_1[0] = 2

A

It replaces the first position in a list with the integer ‘2’

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

what is the syntax of a slice?

A

my_list[start:end]

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

what is the output of…

my_list = [10, 8, 6, 4, 2]
new_list = my_list[:3]
print(new_list)

A

[10, 8, 6]

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

what is a more compact version of…

my_list[start:len(my_list)]

A

my_list[:]

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

remove a slice from the list …

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

A

del list[2:3]

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

what is the output of…

list = [1, 2, 3, 4, 5]
del list[2:3]
print(list)

A

[1, 2, 4, 5]

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

what does this do? …

del my_list

A

deletes the whole list, not just it’s elements

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

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)

A

False
True
True

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

Write a program that finds the largest valur element in a list

A

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)

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

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)

A

[“C”]

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

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)

A

[‘B’, ‘C’]

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

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)

A

[]

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

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

[‘A’, ‘B’, ‘C’]

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

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

A

in
not in
not in
in

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