Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'element': 0.07; 'subject:code': 0.07; 'blue': 0.09; 'chunk': 0.09; 'pointless': 0.09; 'def': 0.12; '0],': 0.16; 'abusing': 0.16; 'chained': 0.16; 'collections': 0.16; 'data)': 0.16; 'deque': 0.16; 'iterable': 0.16; 'itertools': 0.16; 'subject:simple': 0.16; 'throw': 0.16; 'true:': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; "python's": 0.19; 'import': 0.22; 'bonus': 0.22; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'points': 0.29; 'thus': 0.29; 'message-id:@mail.gmail.com': 0.30; 'away.': 0.31; 'this.': 0.32; 'raw': 0.33; 'to:name:python-list': 0.33; 'skip:d 20': 0.34; 'something': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'instances': 0.36; 'yield': 0.36; 'so,': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'remove': 0.60; 'chain': 0.60; 'simple,': 0.60; 'skip:l 50': 0.60; 'course': 0.61; 'here': 0.66; 'repeat': 0.74; 'premature': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:content-type; bh=KN/VRhf+zQPvQz8u4uBQZZQvnLt25nALGgw3E8Hs3oU=; b=W9JCVaULa78wKESCpw6oX225GKwce5ybe8HPZ+pxATNz7J1c9FrI621iUae6r6GTgL MdEukfN8W3oQp0/29jehLWDMSCLzf9JR/v+yusBnVIB4Lc4U/nEoHywJ1hqY5S7elBxa 4ED+GpRaPK9u4sWR+P6UX0HIt/fpvZSI+QYXz4Nwp0iUTWp9po+xzyFfzSQ2otxP/e9v Q3KkxG8rkDVkkzlfMmXyhR8BEXpHNx34RTqZk5X5Kp6418S2+5ZCM1/bVKA8WHfSzDNz L/49ao4GdKT/r4YPn8rps8srsofy6MZhzdufHtYX9LBtRYmy/sBvOGD1FbXmivVhiHw2 HI0w== X-Received: by 10.112.182.39 with SMTP id eb7mr16111376lbc.30.1373510288335; Wed, 10 Jul 2013 19:38:08 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: References: From: Joshua Landau Date: Thu, 11 Jul 2013 03:37:28 +0100 X-Google-Sender-Auth: IX8ix-UyxqI2Yua3zDge9m4P9XQ Subject: Re: Stupid ways to spell simple code To: python-list Content-Type: text/plain; charset=UTF-8 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373510291 news.xs4all.nl 15896 [2001:888:2000:d::a6]:37625 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50404 On 30 June 2013 07:06, Chris Angelico wrote: > So, here's a challenge: Come up with something really simple, and > write an insanely complicated - yet perfectly valid - way to achieve > the same thing. Bonus points for horribly abusing Python's clean > syntax in the process. This occurred to me out of the blue while working on something related. Here's a way to remove all instances of an element from an iterable. It's remarkably fast for it's course of action: from collections import deque from itertools import chain exhaust_iterable = deque(maxlen=0).extend def split_on(data, sentinel): chained = data = iter(data) while True: chunk = iter(chained.__next__, sentinel) yield chunk # Uses at least one item from chained, so "chained" and "data" are equivilant after this. # This is important as "data" is raw and thus will not get bogged down by pointless chain wrappings. # Yes, that is a premature optimisation. Go away. exhaust_iterable(chunk) # Throw StopIteration if not empty chained = chain([next(data)], data) def remove_all(iterable, victim): return list(chain.from_iterable(split_on(iterable, victim))) print(remove_all([1, 2, 1, 1, 0, 1, 2, 1, 1, 2, 2, 1, 0], 1)) and here it is again: from itertools import chain, repeat def remove_all(iterable, victim): iterable = iter(iterable) return list(chain.from_iterable(iter(chain([next(iterable)], iterable).__next__, victim) for _ in repeat(...))) print(remove_all([1, 2, 1, 1, 0, 1, 2, 1, 1, 2, 2, 1, 0], 1))