Find the nearest number to zero in the list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Find the nearest number to zero in the list

myList = [1,-2,-5,8,9]

3rd Feb 2022, 11:11 AM
Manoj Bhaagam
5 Answers
+ 7
Manoj Bhaagam , before we can help you, please post your code here. we need to see what the issue is. thanks!
3rd Feb 2022, 11:32 AM
Lothar
Lothar - avatar
+ 7
ravilnicki , very nice code !!!
4th Feb 2022, 7:21 AM
Lothar
Lothar - avatar
+ 3
Hello Manoj Bhaagam You can use the abs() function to solve this problem. abs(0-n) gives you the distance between 0 and n. e.g. abs(0-8) = 8 abs(0-(-2)) = 2 8 > 2 -> -2 is closer to 0 than 8
3rd Feb 2022, 2:28 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
ravilnicki Cool. I also didn't know that you can use keys.
4th Feb 2022, 2:11 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
ravilnicki Of course, you are absolutely right. lol 🙈
3rd Feb 2022, 6:05 PM
Denise Roßberg
Denise Roßberg - avatar