Topic 7 - List Type in Python Flashcards
(10 cards)
Are lists mutable or immutable?
Lists are mutable, i.e. the contents can be changed.
What code is used to create an empty list?
empty_list = list()
What two operators can be used to test if something is in a list or not?
- ) in
2. ) not in
Explain what is happening with the following code.
empty_list = list()
empty.list.append(5)
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 would you remove the last element in a list?
Use the pop method.
last_elem = empty.list.pop()
How would you add the elements of one list to another, WITHOUT, creating a list within a list, what operator would you use?
Use the extend method.
empty_list.extend( [6,7] )
How would you remove specific elements from a list using the pop method, for example, the first element?
first_elem = empty_list.pop(0)
What method is used to add a specific element to a list at a specific position?
empty_list.insert(0, 10)
In the example above, the value of 10 would be added at index zero.
How would you remove a certain value from a list, for example, the value of 7?
empty_list.remove(7)
What method is used to remove all values from a list?
empty_list.clear()