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: 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; '->': 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; '>>>': 0.24; 'received:65.55.116': 0.24; 'question': 0.24; '>': 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 To: "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: <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com>, , <1ba2ceec-f778-4c95-bf98-260fc48b4c3f@googlegroups.com>, , , , , , <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --_52204515-30dd-478a-9c18-bbf70ff13f73_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable > From: steve+comp.lang.python@pearwood.info > Subject: Re: Beginner question > Date: Tue=2C 4 Jun 2013 12:25:27 +0000 > To: python-list@python.org >=20 > On Tue=2C 04 Jun 2013 14:53:29 +0300=2C Carlos Nepomuceno wrote: >=20 > > That's exactly the same! > >>>>dict(**{a:0=2Cb:1})=3D=3D{a:0=2Cb:1} > > True >=20 >=20 > Of course it is. Isn't that what you wanted? Indeed! But that form isn't economically viable as you noted. >=20 > It's also a waste of time=2C because you create a dict literal using {}= =2C=20 > then unpack it into keyword arguments=2C then call dict() to create a new= =20 > dict with the same content. Rather like doing this: >=20 > n =3D int(str(42)) >=20 > only even more expensive. >=20 >=20 > > Are there any benefits from using dict() instead of {}? >=20 > Of course there are. {} can be used for creating dict literals=2C which=20 > means you are limited to key/values that you explicitly include. dict()= =2C=20 > on the other hand=2C has a rich set of constructor APIs: >=20 > py> help(dict) >=20 > Help on class dict in module builtins: >=20 > class dict(object) > | dict() -> new empty dictionary > | dict(mapping) -> new dictionary initialized from a mapping object's > | (key=2C value) pairs > | dict(iterable) -> new dictionary initialized as if via: > | d =3D {} > | for k=2C v in iterable: > | d[k] =3D v > | dict(**kwargs) -> new dictionary initialized with the name=3Dvalue pa= irs > | in the keyword argument list. For example: dict(one=3D1=2C two= =3D2) >=20 >=20 > py> dict(zip('abcd'=2C range(4))=2C x=3D23=2C y=3D42=2C z=3D999) > {'a': 0=2C 'c': 2=2C 'b': 1=2C 'd': 3=2C 'y': 42=2C 'x': 23=2C 'z': 999} Awesome! Now I can do it just like that: >>> dict([(chr(ord('a')+x)=2Cx) for x in range(2)]) {'a': 0=2C 'b': 1} Thanks a lot! =3B) = --_52204515-30dd-478a-9c18-bbf70ff13f73_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable


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

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

Awesome! Now I can do = it just like that:

>=3B>=3B>=3B dict([(chr(ord('a')+x)=2Cx) fo= r x in range(2)])
{'a': 0=2C 'b': 1}

Thanks a lot! =3B)

= --_52204515-30dd-478a-9c18-bbf70ff13f73_--