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


Groups > comp.lang.python > #46888

RE: Beginner question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <carlosnepomuceno@outlook.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'argument': 0.05; 'beginner': 0.05; 'explicitly': 0.05; 'arguments,': 0.09; 'constructor': 0.09; 'literal': 0.09; '{},': 0.09; 'subject:question': 0.10; '(key,': 0.16; '-&gt;': 0.16; '23,': 0.16; '42,': 0.16; 'dict': 0.16; 'include.': 0.16; 'iterable:': 0.16; 'literals,': 0.16; 'lot!': 0.16; "object's": 0.16; 'pairs': 0.16; 'received:65.55.116.7': 0.16; 'unpack': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'module': 0.19; '>>>': 0.22; '+0000': 0.22; 'to:name:python-list@python.org': 0.22; 'creating': 0.23; '&gt;&gt;&gt;': 0.24; 'received:65.55.116': 0.24; 'question': 0.24; '&gt;': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'waste': 0.30; 'are.': 0.31; 'initialized': 0.31; 'class': 0.32; 'date:': 0.34; 'skip:d 20': 0.34; 'but': 0.35; 'there': 0.35; 'keyword': 0.36; 'doing': 0.36; 'thanks': 0.36; 'email addr:python.org': 0.37; 'list.': 0.37; 'mapping': 0.38; 'rich': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'subject:': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'is.': 0.60; 'new': 0.61; 'course': 0.61; 're:': 0.63; 'more': 0.64; 'email name:python-list': 0.65; 'dict()': 0.84; 'viable': 0.84; 'carlos': 0.91; 'hand,': 0.93; '2013': 0.98
X-TMN [ZgE+r8GJslO+n1Ra8Q13euvH2m2XaRKY]
X-Originating-Email [carlosnepomuceno@outlook.com]
Content-Type multipart/alternative; boundary="_52204515-30dd-478a-9c18-bbf70ff13f73_"
From Carlos Nepomuceno <carlosnepomuceno@outlook.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: Beginner question
Date Tue, 4 Jun 2013 15:37:22 +0300
Importance Normal
In-Reply-To <51addcb7$0$29966$c3e8da3$5496439d@news.astraweb.com>
References <51addcb7$0$29966$c3e8da3$5496439d@news.astraweb.com>
MIME-Version 1.0
X-OriginalArrivalTime 04 Jun 2013 12:37:22.0603 (UTC) FILETIME=[42B06FB0:01CE6120]
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.2642.1370349451.3114.python-list@python.org> (permalink)
Lines 123
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1370349451 news.xs4all.nl 15930 [2001:888:2000:d::a6]:54202
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:46888

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

> From: steve+comp.lang.python@pearwood.info
> Subject: Re: Beginner question
> Date: Tue, 4 Jun 2013 12:25:27 +0000
> To: python-list@python.org
> 
> On Tue, 04 Jun 2013 14:53:29 +0300, Carlos Nepomuceno wrote:
> 
> > That's exactly the same!
> >>>>dict(**{a:0,b:1})=={a:0,b:1}
> > True
> 
> 
> Of course it is. Isn't that what you wanted?

Indeed! But that form isn't economically viable as you noted.
> 
> It's also a waste of time, because you create a dict literal using {}, 
> then unpack it into keyword arguments, then call dict() to create a new 
> dict with the same content. Rather like doing this:
> 
> n = int(str(42))
> 
> only even more expensive.
> 
> 
> > Are there any benefits from using dict() instead of {}?
> 
> Of course there are. {} can be used for creating dict literals, which 
> means you are limited to key/values that you explicitly include. dict(), 
> on the other hand, has a rich set of constructor APIs:
> 
> py> help(dict)
> 
> Help on class dict in module builtins:
> 
> class dict(object)
>  |  dict() -> new empty dictionary
>  |  dict(mapping) -> new dictionary initialized from a mapping object's
>  |      (key, value) pairs
>  |  dict(iterable) -> new dictionary initialized as if via:
>  |      d = {}
>  |      for k, v in iterable:
>  |          d[k] = v
>  |  dict(**kwargs) -> new dictionary initialized with the name=value pairs
>  |      in the keyword argument list.  For example:  dict(one=1, two=2)
> 
> 
> py> dict(zip('abcd', range(4)), x=23, y=42, z=999)
> {'a': 0, 'c': 2, 'b': 1, 'd': 3, 'y': 42, 'x': 23, 'z': 999}

Awesome! Now I can do it just like that:

>>> dict([(chr(ord('a')+x),x) for x in range(2)])
{'a': 0, 'b': 1}

Thanks a lot! ;)

 		 	   		  

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Beginner question eschneider92@comcast.net - 2013-06-03 20:39 -0700
  RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 09:46 +0300
    Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:53 -0700
      RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 11:17 +0300
  Re: Beginner question Anssi Saari <as@sci.fi> - 2013-06-04 10:45 +0300
    Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:57 -0700
      Re: Beginner question Chris Angelico <rosuav@gmail.com> - 2013-06-04 18:24 +1000
      Re: Beginner question Peter Otten <__peter__@web.de> - 2013-06-04 11:23 +0200
      RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:23 +0300
        Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:35 +0000
          RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:51 +0300
            Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-07 02:21 +0000
      RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 12:34 +0100
      RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:53 +0300
        Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:25 +0000
          RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:37 +0300
          RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:47 +0100
      RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:15 +0100
      Re: Beginner question Mitya Sirenef <msirenef@lightbird.net> - 2013-06-04 13:16 -0400
  Re: Beginner question Larry Hudson <orgnut@yahoo.com> - 2013-06-04 02:13 -0700
    Re: Beginner question Roy Smith <roy@panix.com> - 2013-06-04 09:16 -0400
  Re: Beginner question Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-04 20:43 +0100
  Re: Beginner question eschneider92@comcast.net - 2013-06-05 20:42 -0700

csiph-web