+ 2

Find the nearest number to zero in the list

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

3rd Feb 2022, 11:11 AM
Manoj Bhaagam
7 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