dict.keys() and dict.values() are always the same order, is it?

Discussion in 'Python' started by Menghan Zheng, Apr 20, 2010.

  1. Hello!

    Is it assured the following statement is always True?
    If it is always True, in which version, python2.x or python3.x?

    >>> a = dict()

    ....
    >>> assert(a.values == [a[k] for k in a.keys()])

    --> ?


    Menghan Zheng
     
    Menghan Zheng, Apr 20, 2010
    #1
    1. Advertising

  2. Menghan Zheng

    alex23 Guest

    On Apr 20, 1:03 pm, Menghan Zheng <> wrote:
    > Is it assured the following statement is always True?
    > If it is always True, in which version, python2.x or python3.x?


    I believe its an implementation detail and should not be relied on. If
    you need consistent ordering, use an OrderedDict[1] or sort() the
    lists beforehand.
     
    alex23, Apr 20, 2010
    #2
    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.
Similar Threads
  1. Cameron Simpson
    Replies:
    6
    Views:
    355
    alex23
    Apr 21, 2010
  2. Henrik Faber
    Replies:
    8
    Views:
    219
    Ethan Furman
    Jul 26, 2012
  3. Ethan Furman

    Python 3: dict & dict.keys()

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    4
    Views:
    216
    Steven D'Aprano
    Jul 25, 2013
  4. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    94
    Neil Cerutti
    Jul 24, 2013
  5. Oscar Benjamin

    Re: Python 3: dict & dict.keys()

    Oscar Benjamin, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    108
    Oscar Benjamin
    Jul 24, 2013
Loading...

Share This Page