Exercise: Python

Questions for: Lists

What is the correct way to create a new list by multiplying each element of an existing list by 2 in Python?
A:
new_list = list * 2
B:
new_list = [element * 2 for element in old_list]
C:
new_list = old_list.multiply(2)
D:
new_list = old_list * 2
Answer: B
The correct way to create a new list by multiplying each element of an existing list by 2 is using a list comprehension as shown in option B.
Which method is used to remove the first occurrence of a specific element from a list?
A:
remove(element)
B:
delete(element)
C:
pop(element)
D:
discard(element)
Answer: A
The remove(element) method is used to remove the first occurrence of a specific element from a list.
What is the purpose of the count method in Python lists?
A:
To count the number of occurrences of a specific element.
B:
To count the total number of elements in the list.
C:
To count the number of unique elements in the list.
D:
To count the number of elements satisfying a condition.
Answer: A
The count method in Python lists is used to count the number of occurrences of a specific element.
How can you reverse the order of elements in a list in Python?
A:
reverse()
B:
list.reverse()
C:
reversed(list)
D:
list.reversed()
Answer: B
The list.reverse() method is used to reverse the order of elements in a list in-place.
Which of the following methods is used to insert an element at a specific index in a list?
A:
add(index, element)
B:
insert(index, element)
C:
append(index, element)
D:
push(index, element)
Answer: B
The insert(index, element) method is used to insert an element at a specific index in a list.
Ad Slot (Above Pagination)
Quiz