Difference between list() and [] with dictionaries

Aahz aahz at pythoncraft.com
Sat May 16 16:45:52 EDT 2009


In article <mailman.247.1242441988.8015.python-list at python.org>,
Ned Deily  <nad at acm.org> wrote:
>In article 
><fe3354e50905151817ie4df792hebec03ae42eca25c at mail.gmail.com>,
> Sam Tregar <sam at tregar.com> wrote:
>>
>> Can anyone explain why this creates a list containing a
>> dictionary:
>>   [{'a': 'b', 'foo': 'bar'}] 
>> But this creates a list of keys of the dictionary:
>>   list({ "a": "b", "foo": "bar" })
>
>The first example is a list, expressed as a list display literal, 
>containing one object: a dictionary, expressed as a dictionary display 
>literal.
>
>The second example is a call to the built-in function "list",  [...]

Actually, list() is not a function:

>>> list
<type 'list'>

Rather, ``list`` is an object (specifically a ``type`` object) with a
__call__() method.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In 1968 it took the computing power of 2 C-64's to fly a rocket to the moon.
Now, in 1998 it takes the Power of a Pentium 200 to run Microsoft Windows 98.
Something must have gone wrong."  --/bin/fortune



More information about the Python-list mailing list