Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Vito De Tullio Newsgroups: comp.lang.python Subject: Re: Drowning in a teacup? Date: Sat, 02 Apr 2016 07:45:31 +0200 Lines: 27 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de rZ7nykWyqkZhVkCaRayHXgWo/X/0BIBU0Z+ufzmnwbHg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'matches': 0.07; 'strings.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'key?': 0.16; 'keyword,': 0.16; 'list)': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'element': 0.18; '(in': 0.18; '>>>': 0.20; 'elements': 0.23; 'sort': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; '(new': 0.29; 'post': 0.31; "can't": 0.32; 'list': 0.34; 'something': 0.35; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'list.': 0.37; 'front': 0.38; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'snap': 0.84; 'received:2': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 2.193.67.160 User-Agent: KNode/4.14.10 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:106274 Fillmore wrote: > I need to scan a list of strings. If one of the elements matches the > beginning of a search keyword, that element needs to snap to the front > of the list. I know this post regards the function passing, but, on you specific problem, can't you just ... sort the list with a custom key? something like (new list) >>> sorted(['no_a', 'yes_c', 'no_b', 'yes_z', 'no_y', 'yes_x'], ... key=lambda e: not e.startswith('yes')) ['yes_c', 'yes_z', 'yes_x', 'no_a', 'no_b', 'no_y'] or (in place) >>> l = ['no_a', 'yes_c', 'no_b', 'yes_z', 'no_y', 'yes_x'] >>> l.sort(key=lambda e: not e.startswith('yes')) >>> l ['yes_c', 'yes_z', 'yes_x', 'no_a', 'no_b', 'no_y'] -- By ZeD