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


Groups > comp.lang.python > #46888

RE: Beginner question

From Carlos Nepomuceno <carlosnepomuceno@outlook.com>
Subject RE: Beginner question
Date 2013-06-04 15:37 +0300
References <51addcb7$0$29966$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.2642.1370349451.3114.python-list@python.org> (permalink)

Show all headers | 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