Tuple unpacking | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Tuple unpacking

https://code.sololearn.com/cuEUJ988z5Mi/?ref=app Need to find the smallest distance form the given list of tuples

2nd Mar 2021, 1:58 AM
THIRU KUMARAN T R
THIRU KUMARAN T R - avatar
8 Answers
+ 3
David Ashton 's code can be reduced to; b = [] b.extend((x*x+y*y)**0.5 for x, y in points) print(min(b))
2nd Mar 2021, 4:41 AM
ChaoticDawg
ChaoticDawg - avatar
+ 7
I would do it this way b = [] for i, j in points: x, y = i, j a =[(x*x + y*y)**0.5] b.extend(a) print(min(b))
2nd Mar 2021, 2:41 AM
David Ashton
David Ashton - avatar
+ 4
ChaoticDawg Nicely done! And pythonic. 👏 Might as well reduce it all the way print(min((x*x+y*y)**0.5 for x, y in points)) 😁
2nd Mar 2021, 5:07 AM
David Ashton
David Ashton - avatar
+ 2
David Ashton 😆🤣😂 lol, nice
2nd Mar 2021, 5:12 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
print(min(b)) Also pit b on outside of for loop, otherwise it resets
2nd Mar 2021, 2:16 AM
Satrio Bayu Pradhipta
Satrio Bayu Pradhipta - avatar
+ 1
b initialization should happens outside for loops. What happens in your code is, i=0-> b=[] a= distance calc b=[a0] i=1-> b=[] a= calc b=[a1] i=2-> b=[] a=calc b=[a3] and so on. B never store every results of iteration, only its last iteration got stored. Hence my suggestion to initialize b outside of for loops
2nd Mar 2021, 2:42 AM
Satrio Bayu Pradhipta
Satrio Bayu Pradhipta - avatar
0
Satrio Bayu Pradhipta I tried it it's printing 90 but it should print 43 When I print inside the for loop I'm able to print all the values of a But couldn't print the minimum value in a
2nd Mar 2021, 2:32 AM
THIRU KUMARAN T R
THIRU KUMARAN T R - avatar
0
David Ashton I thought i can take the both values of the tuple inside the for loop thanks for rectifying the error sir!!
2nd Mar 2021, 2:52 AM
THIRU KUMARAN T R
THIRU KUMARAN T R - avatar