Write the Python statement for each of the following tasks using BUILTIN functions/methods only:
(i) To remove the last element from the list L2 and store it in a variable x.
Answer:
Answer by student
The Python statement to remove the last element from the list L2 and store it in a variable x is:
x = L2.pop()
Detailed answer by teachoo
-lock-
- To remove the last element from the list L2 and store it in a variable x, we can use the pop() method of the list object.
- The pop() method takes an optional index as an argument and removes and returns the element at that index from the list.
- If no index is given, it removes and returns the last element of the list.
- Therefore, we can write x = L2.pop() to remove the last element from L2 and store it in x.
So, the correct answer is x = L2.pop()
-endlock-