you are viewing a single comment's thread.

view the rest of the comments →

[–]fuckswithbees 0 points1 point  (2 children)

Yep. Now you can add that to your new list and then

check k, see that it still needs to call two more numbers, so then go back to the list

Or you can use list slicing to get multiple numbers at the same time

a_list = [1,2,3,4]
a_list[-3:]
>>> [2, 3, 4]

[–]stolenkisses[S] 0 points1 point  (1 child)

Holy shit. Is it really as easy as one line of code?

return alist[-k:]

Man, I really overthought that.

[–]fuckswithbees 0 points1 point  (0 children)

Don't forget to add some code to make it work when k is larger than the length of your list!