+ 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.

6th Sep 2020, 9:37 AM
SùñtÎsh
SùñtÎsh - avatar
6 Respostas
+ 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)
6th Sep 2020, 9:57 AM
Namit Jain
Namit Jain - avatar
+ 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.
6th Sep 2020, 9:48 AM
Russ
Russ - avatar
+ 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.
6th Sep 2020, 9:46 AM
Russ
Russ - avatar
6th Sep 2020, 9:46 AM
SùñtÎsh
SùñtÎsh - avatar
+ 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.
6th Sep 2020, 9:57 AM
viknesh vikky
viknesh vikky - avatar
+ 2
How can you
8th Sep 2020, 4:59 AM
Yaduvanshi Rohan
Yaduvanshi Rohan - avatar