Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'array.': 0.16; 'collections': 0.16; 'deque': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'elements': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'array': 0.29; 'subject:list': 0.30; 'continues': 0.31; 'display': 0.35; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'such': 0.63; 'grab': 0.64; 'email addr:live.com': 0.68; 'hundred': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Wrapping around a list in Python. Date: Mon, 16 Dec 2013 10:15:48 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b396.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387185319 news.xs4all.nl 2963 [2001:888:2000:d::a6]:38774 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62025 shengjie.shengjie@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? Use a deque: >>> from collections import deque >>> last_four = deque(maxlen=4) >>> for i in range(10): ... last_four.append(i) ... >>> last_four deque([6, 7, 8, 9], maxlen=4) >>> last_four.extend(range(100, 200)) >>> last_four deque([196, 197, 198, 199], maxlen=4) >>> last_four.append(42) >>> last_four deque([197, 198, 199, 42], maxlen=4)