Re: Dict comp help

Discussion in 'Python' started by Rob Day, Jan 24, 2013.

  1. Rob Day

    Rob Day Guest

    On 24 January 2013 21:11, Oscar Benjamin <> wrote:
    >>>> l = [{'a': 'xx', 'b': 'yy', 'c': 'zz'}, {'a': 'dd', 'b': 'ee', 'c': 'ff'}]
    >>>> dict(d.values()[:2] for d in l)

    > {'xx': 'zz', 'dd': 'ff'}


    Python doesn't guarantee any ordering of items in a dictionary; {'a':
    'xx', 'b': 'yy', 'c': 'zz'}.values()[:2] may not return ['xx', 'zz']
    in different Python implementations or future versions (though it
    seems to work consistently in CPython 2.7). See
    http://docs.python.org/2/library/stdtypes.html#dict.items.

    --
    Robert K. Day
    Rob Day, Jan 24, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page