In article <[email protected]>, Sam Tregar <[email protected]> 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. <http://docs.python.org/reference/expressions.html#atoms> The second example is a call to the built-in function "list", which takes a single iterable as an argument and returns "a list whose items are the same and in the same order as iterableĀs items". In this case, that single argument is a dictionary. In an iteration context, the iterator obtained for a dictionary iterates over the dictionary's keys. The second example is essentially shorthand for: list({ "a": "b", "foo": "bar" }.iterkeys()) <http://docs.python.org/library/functions.html#list> <http://docs.python.org/library/stdtypes.html#mapping-types-dict> -- Ned Deily, [email protected] -- http://mail.python.org/mailman/listinfo/python-list
