Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3633

List comprehension vs filter()

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'string.': 0.04; 'dictionary': 0.07; 'method,': 0.07; 'python': 0.07; 'context:': 0.09; 'interpreter,': 0.09; 'interpreter.': 0.09; 'subject:()': 0.09; 'dictionaries': 0.16; 'hints,': 0.16; 'please!': 0.16; 'subject:List': 0.20; 'seeing': 0.21; 'received:209.85.214.174': 0.23; 'received:mail-iw0-f174.google.com': 0.23; 'restrict': 0.23; 'version': 0.25; "i'm": 0.26; 'chris': 0.27; 'message- id:@mail.gmail.com': 0.28; 'list': 0.30; 'to:addr:python-list': 0.32; 'idea': 0.32; 'difference': 0.35; 'doing': 0.36; 'problem.': 0.36; 'two': 0.37; 'skip:o 20': 0.37; 'received:209.85': 0.37; 'either': 0.37; 'element': 0.38; 'resulting': 0.38; 'received:google.com': 0.38; 'ways': 0.38; 'completely': 0.38; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'received:209': 0.39; 'works': 0.40; 'header:Received:5': 0.40; 'job.': 0.60; 'oddly,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=FbnsuDaOPF9BOu2cNrqMA1h7AV48EiL0+vSv+Gah408=; b=SXcVvvdY4jjKCjr3JuiBAFVVlwbCUe5Kj4DpH7Ox8UEslGPCZb3eJ1jihp+eZTyBXE LcLxkRnyaRX0dO3RtUxnhoeis27MdpOmdBn2saO1qijZaKtTUIzTl4cm1usvycV4gfHz WsjEYnLVwvsTowCUga2XHNVYMcjH94rJmqjmA=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=mAhEYVkSgPRWqDNAzcY9UxZNouoEMoqN0+d/WUMXcZ8Y0tn0QCsOgKyoCCryhKCx3u fIalL8+qyXL6bVobuGqc2OJQmH/FjY0JGUov1BmpkCE0yXMp2+aMJtqYcGbcRD9AJRsy maCi9gJ4035VfdSKBRZDgSfStDriN7XLjMFU8=
MIME-Version 1.0
Date Wed, 20 Apr 2011 13:10:21 +1000
Subject List comprehension vs filter()
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.603.1303269025.9059.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 82.94.164.166
X-Trace 1303269025 news.xs4all.nl 41103 [::ffff:82.94.164.166]:45297
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3633

Show key headers only | View raw


Context: Embedded Python interpreter, version 2.6.6

I have a list of dictionaries, where each dictionary has a "type"
element which is a string. I want to reduce the list to just the
dictionaries which have the same "type" as the first one.

lst=[{"type":"calc",...},{"type":"fixed",...},{"type":"calc",...},...]

I'm seeing a weird difference between two otherwise-equivalent-looking
ways of doing the job.

type=lst[0]["type"].lower()

lst=filter(lambda x: x["type"].lower()==type,lst) # Restrict to that one type

lst=[i for i in lst if i["type"].lower()==type] # Restrict to that one type

If I use the filter() method, the resulting list is completely empty.
If I use the list comprehension, it works perfectly. Oddly, either
version works in the stand-alone interpreter.

I have no idea where to start looking for the problem. Hints, please!

Chris Angelico

Back to comp.lang.python | Previous | NextNext in thread | Find similar


Thread

List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-20 13:10 +1000
  Re: List comprehension vs filter() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-20 10:16 +0000
    Re: List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-20 21:25 +1000
  Re: List comprehension vs filter() Peter Otten <__peter__@web.de> - 2011-04-20 12:41 +0200
    Re: List comprehension vs filter() Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-20 08:44 -0600
    Re: List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-21 04:03 +1000
    Re: List comprehension vs filter() Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-20 12:35 -0600

csiph-web