Topic 7 - List Type in Python Flashcards

1
Q

Are lists mutable or immutable?

A

Lists are mutable, i.e. the contents can be changed.

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

What code is used to create an empty list?

A

empty_list = list()

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

What two operators can be used to test if something is in a list or not?

A
  1. ) in

2. ) not in

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

Explain what is happening with the following code.

empty_list = list()
empty.list.append(5)

A

First, a empty list variable is created.

Then, the value of 5 is added to the list. If other existing elements existed, the value of 5 would be added to the end of the list.

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

How would you remove the last element in a list?

A

Use the pop method.

last_elem = empty.list.pop()

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

How would you add the elements of one list to another, WITHOUT, creating a list within a list, what operator would you use?

A

Use the extend method.

empty_list.extend( [6,7] )

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

How would you remove specific elements from a list using the pop method, for example, the first element?

A

first_elem = empty_list.pop(0)

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

What method is used to add a specific element to a list at a specific position?

A

empty_list.insert(0, 10)

In the example above, the value of 10 would be added at index zero.

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

How would you remove a certain value from a list, for example, the value of 7?

A

empty_list.remove(7)

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

What method is used to remove all values from a list?

A

empty_list.clear()

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