KnowledgeBoat Logo
|

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)

Python List Manipulation

7 Likes

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.

Answered By

1 Like


Related Questions