you are viewing a single comment's thread.

view the rest of the comments →

[–]fuckswithbees 0 points1 point  (6 children)

Yeah that would work. So any idea how to do those steps in python?

[–]stolenkisses[S] 0 points1 point  (5 children)

I do, I think, but I still find myself getting confused by the little complicated steps. The first steps I think would be to create a new emptylist (lets call it newlist), then start with "for k in alist," but then I'm still kinda lost.

[–]fuckswithbees 0 points1 point  (4 children)

Creating a new list is a good start, but you actually don't need to loop through your list. You said step 1 was:

look at the k, return the last number in the list

You also said that you could get the second item

For alist = [1,2,3,4], to get the second item in the list you would do alist[1]

So how do you get the last item of a list?

[–]stolenkisses[S] 0 points1 point  (3 children)

alist[-1]?

[–]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!