What will be the output of the following Python code snippet? [code]l=[1, 2, 4, 5, 2, 'xy', 4] set(l) l[/code]

Tiên Hương

New member
What will be the output of the following Python code snippet?
Code:
l=[1, 2, 4, 5, 2, 'xy', 4]
set(l)
l
A.
{1, 2, 4, 5, 2, ‘xy’, 4}
[1, 2, 4, 5, 2, ‘xy’, 4]
B.
{1, 2, 4, 5, ‘xy’}
[1, 2, 4, 5, 2, ‘xy’, 4]
C.
{1, 5, ‘xy’}
[1, 5, ‘xy’]
D.
{1, 2, 4, 5, ‘xy’}
[1, 2, 4, 5, ‘xy’]
 
Answer: b
Explanation: In the code shown above, the function set(l) converts the given list into a set. When this happens, all the duplicates are automatically removed. Hence the output is: {1, 2, 4, 5, ‘xy’}. On the other hand, the list l remains unchanged. Therefore the output is: [1, 2, 4, 5, 2, ‘xy’, 4].
Note that the order of the elements may not be the same.
 

Tìm kiếm

Back
Top