Exercise: Lists

Questions for: Lists

How can you remove all occurrences of a specific element from a list?
A:
list.remove_all(element)
B:
list.delete(element)
C:
list.remove(element, all=True)
D:
list = [x for x in list if x != element]
Answer: D
To remove all occurrences of a specific element, you can use a list comprehension to filter out the unwanted element.
What is the purpose of the extend method in Python lists?
A:
Adds a specified element to the list.
B:
Extends the list by appending elements from an iterable.
C:
Increases the length of the list.
D:
Concatenates two lists.
Answer: B
The extend() method in Python lists is used to extend the list by appending elements from an iterable.
What does the copy() method do in Python lists?
A:
Creates a deep copy of the list.
B:
Creates a shallow copy of the list.
C:
Copies the elements to a new list.
D:
Copies the list to another list.
Answer: B
The copy() method in Python lists is used to create a shallow copy of the list.
How can you find the index of the last occurrence of a specific element in a list?
A:
list.last_index(element)
B:
list.index(element, last=True)
C:
list.rindex(element)
D:
list.find_last(element)
Answer: C
The rindex() method is used to find the index of the last occurrence of a specific element in a list.
Which method is used to add an element at the end of a list in Python?
A:
list.append(element)
B:
list.insert(-1, element)
C:
list.add_last(element)
D:
list.push(element)
Answer: A
The append() method is used to add an element at the end of a list in Python.
Ad Slot (Above Pagination)
Quiz