Python Programming - Lists

Exercise : Lists - General Questions
  • Lists - General Questions
21.
Which method is used to add an element at the end of a list in Python?
list.append(element)
list.insert(-1, element)
list.add_last(element)
list.push(element)
Answer: Option
Explanation:
The append() method is used to add an element at the end of a list in Python.

22.
How can you find the index of the last occurrence of a specific element in a list?
list.last_index(element)
list.index(element, last=True)
list.rindex(element)
list.find_last(element)
Answer: Option
Explanation:
The rindex() method is used to find the index of the last occurrence of a specific element in a list.

23.
What does the copy() method do in Python lists?
Creates a deep copy of the list.
Creates a shallow copy of the list.
Copies the elements to a new list.
Copies the list to another list.
Answer: Option
Explanation:
The copy() method in Python lists is used to create a shallow copy of the list.

24.
What is the purpose of the extend method in Python lists?
Adds a specified element to the list.
Extends the list by appending elements from an iterable.
Increases the length of the list.
Concatenates two lists.
Answer: Option
Explanation:
The extend() method in Python lists is used to extend the list by appending elements from an iterable.

25.
How can you remove all occurrences of a specific element from a list?
list.remove_all(element)
list.delete(element)
list.remove(element, all=True)
list = [x for x in list if x != element]
Answer: Option
Explanation:
To remove all occurrences of a specific element, you can use a list comprehension to filter out the unwanted element.