<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline"><td><nobr><b><tt id="l2h-21" xml:id="l2h-21" class="function">dict</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>arg</var><big>]</big><var></var>)</td></tr></tbody></table>
Return a new dictionary initialized from an optional positional
argument or from a set of keyword arguments.
If no arguments are given, return a new empty dictionary.
If the positional argument <var>arg</var> is a mapping object, return a dictionary
mapping the same keys to the same values as does the mapping object.<br><br><br>But why doesn't the optional positional argument arg in this case, not being a mapping type, get evaluated?: dict(thing=1)<br><br>And even if it makes sense for it not to be evaluated, wouldn't it be better for dict() to complain that it didn't get a string or an int as it expects for a keyword argument? Maybe I am missing the use case, so far it just seems strange to force the keyword to a string.<br><br>-Sam<br><div><br></div><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><font size="2" face="Tahoma"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> bob gailer <bgailer@gmail.com><br><b><span style="font-weight: bold;">To:</span></b> wormwood_3 <wormwood_3@yahoo.com><br><b><span style="font-weight: bold;">Cc:</span></b> Python
Tutorlist <tutor@python.org><br><b><span style="font-weight: bold;">Sent:</span></b> Wednesday, January 21, 2009 11:25:12 PM<br><b><span style="font-weight: bold;">Subject:</span></b> Re: [Tutor] dict() versus {}<br></font><br>
wormwood_3 wrote:
<blockquote type="cite">
<div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">Hmm,
looked through the latest docs, in the sections on dictionary types,
don't see examples that point to this case well. Can you link to what
you had in mind?<br>
</div>
</blockquote>
<br>
2.1 Built-in Functions <br>
...<br>
dict( [mapping-or-sequence]) <br>
...<br>
these all return a dictionary equal to <code>{"one": 2, "two": 3}</code>:
<br>
...<br>
dict(one=2, two=3) <br>
<br>
<blockquote type="cite">
<div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">
<div>
<div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><br>
<div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><font size="2" face="Tahoma">
<hr size="1"><b><span style="font-weight: bold;">From:</span></b> bob
gailer <a rel="nofollow" class="moz-txt-link-rfc2396E" ymailto="mailto:bgailer@gmail.com" target="_blank" href="mailto:bgailer@gmail.com"><bgailer@gmail.com></a><br>
<b><span style="font-weight: bold;">To:</span></b> wormwood_3
<a rel="nofollow" class="moz-txt-link-rfc2396E" ymailto="mailto:wormwood_3@yahoo.com" target="_blank" href="mailto:wormwood_3@yahoo.com"><wormwood_3@yahoo.com></a><br>
<b><span style="font-weight: bold;">Cc:</span></b> Python Tutorlist
<a rel="nofollow" class="moz-txt-link-rfc2396E" ymailto="mailto:tutor@python.org" target="_blank" href="mailto:tutor@python.org"><tutor@python.org></a><br>
<b><span style="font-weight: bold;">Sent:</span></b> Wednesday,
January 21, 2009 11:02:35 PM<br>
<b><span style="font-weight: bold;">Subject:</span></b> Re: [Tutor]
dict() versus {}<br>
</font><br>
wormwood_3 wrote:
<blockquote type="cite">
<div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">When
creating a list of dictionaries through a loop, I ran into a strange
issue. I'll let the code talk:<br>
<br>
>>> l = 'i am a special new list'.split()<br>
>>> t = []<br>
>>> for thing in l:<br>
... t.append({thing: 1})<br>
... <br>
>>> t<br>
[{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}]<br>
<br>
This is what I expected. {} says to make a dictionary. Thing, not being
quoted, is clearing a variable, which needs to be evaluated and used as
the key.<br>
<br>
>>> t = []<br>
>>> for thing in l:<br>
... t.append(dict(thing=1))<br>
... <br>
>>> t<br>
[{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1},
{'thing': 1}]<br>
<br>
This was what threw me. Why would the dict() function not evaluate
thing? How can it take it as a literal string without quotes?</div>
</blockquote>
I suggest you look dict up in the Python documentation. There it shows
the result you got as an example. When in doubt read the manual.<br>
<br>
<div class="moz-signature">-- <br>
Bob Gailer<br>
Chapel Hill NC<br>
919-636-4239</div>
</div>
</div>
</div>
</div>
<pre><hr size="4" width="90%">
_______________________________________________
Tutor maillist - <a rel="nofollow" class="moz-txt-link-abbreviated" ymailto="mailto:Tutor@python.org" target="_blank" href="mailto:Tutor@python.org">Tutor@python.org</a>
<a rel="nofollow" class="moz-txt-link-freetext" target="_blank" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>
</pre>
</blockquote>
<br>
<br>
<div class="moz-signature">-- <br>
Bob Gailer<br>
Chapel Hill NC<br>
919-636-4239</div>
</div></div></div></div></body></html>