Python Programming - Lists

Exercise : Lists - General Questions
  • Lists - General Questions
61.
What does the list.remove(42) method do in Python?
Removes the element with the value 42.
Removes the first occurrence of 42.
Raises an error if the element 42 is not found.
Removes all occurrences of 42.
Answer: Option
Explanation:
The remove() method in Python lists removes the first occurrence of a specific element.

62.
How can you reverse the order of elements in a list without modifying the original list?
reversed_list = list.reverse()
reversed_list = list[::-1]
reversed_list = list.reverse_copy()
reversed_list = list.reverse(True)
Answer: Option
Explanation:
Using slicing [::-1] creates a new list with the reversed order of elements.

63.
What will the list.extend([7, 8, 9]) method do?
Adds a single element [7, 8, 9] to the list.
Adds individual elements 7, 8, 9 to the end of the list.
Merges the list with [7, 8, 9] creating a new list.
Inserts [7, 8, 9] at the beginning of the list.
Answer: Option
Explanation:
The extend() method adds individual elements from the iterable to the end of the list.

64.
How can you find the frequency of each element in a list in Python?
list.count(element)
list.frequency(element)
dict(Counter(list))
list.freq(element)
Answer: Option
Explanation:
Using Counter and dict() can create a dictionary with element frequencies.

65.
How can you create a list containing only the unique elements from another list in Python?
list.distinct()
list.unique()
list(set(list))
list.remove_duplicates()
Answer: Option
Explanation:
Converting a list to a set removes duplicates, and then converting it back to a list creates a new list with unique elements.