Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my list slice not return last 6 values in my problem

I am trying to return the last 6 values of a list but it doesn’t work right. I want it to return [1, 2, 2, 3, 5, 6] but it returns [0, 0, 2, 5, 6] and I can’t see why any help is appreciated.

nums1 = [1,2,3,0,0,0]
nums2 = [2,5,6]

ans = nums1 + nums2
print(sorted(ans[-6:]))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are selecting the last 6 element and then sorting, it’ll be done otherway around.

print(sorted(ans)[-6:])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading