Informatics Practices
Find the output of the following:
L1=[1, 2, 3]
L2=[4, 5, 6]
print(L1+list ("45") )
print(L1.pop())
L1.remove (2)
print(L1)
L1.extend (L2)
print(L2)
Answer
[1, 2, 3, '4', '5']
3
[1]
[4, 5, 6]
Working
In the above code, L1 is defined as [1, 2, 3] and L2 as [4, 5, 6]. The first print(L1 + list("45")) concatenates L1 with the list representation of the string "45", resulting in [1, 2, 3, '4', '5']. The next operation print(L1.pop()) removes and returns the last element of L1, which is 3, printing it. Then, L1.remove(2) removes the element 2 from L1, leaving [1]. Subsequently, L1.extend(L2) appends elements from L2 to L1, modifying it to [1, 4, 5, 6]. Lastly, print(L2) outputs [4, 5, 6], showing that L2 remains unchanged despite being extended into L1.
Related Questions
What is the difference between insert() and append() methods of a list?
Suppose L = [10, ["few", "facts", "fun"], 3, 'Good']
Consider the above list and answer the following:
(a) L[3:]
(b) L[::2]
(c) L[1:2]
(d) L[1] [1]
What will be the output of the following statement?
list1 = [12, 32, 65, 26, 80, 10] list1.sort() print(list1)What will be the output of the following statement?
list1 = [12, 32, 65, 26, 80, 10] sorted(list1) print(list1)