Wrapping around a list in Python.
Dave Angel
davea at davea.name
Mon Dec 16 07:51:49 EST 2013
On Sun, 15 Dec 2013 21:26:49 -0800 (PST), shengjie.shengjie at live.com
wrote:
> The idea is to grab the last 4 elements of the array. However i
have an array that contains a few hundred elements in it. And the
values continues to .append over time. How would i be able to display
the last 4 elements of the array under such a condition?
Your earlier example showed [5, 4, 1, 2] as one of the results, which
is not "the last four". But assuming your goal has now changed and
you really have an array (or list) of a few hundred elements, then
you can just use data [-4:] to get them.
But if you're being imprecise and these values are not really in an
array, then follow Peter ' advice and use a deque. Or fake it with a
list, appending to the end and popping from the beginning.
--
DaveA
More information about the Python-list
mailing list