+ 11
Please explain this question heređđ
https://code.sololearn.com/cRqSlEEtEO40/?ref=app Above code gives error but in challenge it's answer is 2 I confused please explain about this code.
6 Answers
+ 15
The external/outer brackets are ignored if you don't use , (comma)
People generally get confused due to this and think that it is a tuple
(3) == 3 but (3,) becomes a tuple
Similarly,
([1,2,3]) == [1,2,3] So, use comma: ([1,2,3],)
Russ sir is absolutely correct â
Here's another way (ig this was the challenge question)
L=[(1,2,3)]
for (T1,T2,T3) in L:
print(T2)
+ 11
If the code is this:
L=([[1,2,3]])
for (T1,T2,T3) in L:
print(T2)
the answer IS 2. There is a list inside a list in this code.
Also, if the code is this:
L=([1,2,3],)
for (T1,T2,T3) in L:
print(T2)
2 again is correct. There is a list inside a tuple in this code.
+ 8
The answer shouldn't be 2. Make doubly sure you have copied the code correctly and, if so, flag the question as being incorrect.
+ 5
DANIELđthe STARââ I don't understand
+ 5
SÄñtösh Mà rà vi Yes the code should be like as said by Russ.
In your code the value of L is a single list [1,2,3] and so when you use to iterate in for loop, on first iteration the program tries to unpack like this (T1,T2,T3) in 1. As you see the value 1 is not an iterable object it throws error.
But in L = ([[1,2,3]]), on first iteration the program runs it as (T1,T2,T3) in [1,2,3]. Here it is unpacking the list object and assigns each variable a value from the list.
So the result will be printed as 2.
+ 2
How can you