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. Advertisements

  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. Advertisements

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. Dirkjan Ochtman

    Creating dict from keys and values

    Dirkjan Ochtman, May 29, 2005, in forum: Python
    Replies:
    1
    Views:
    346
  2. Cameron Simpson
    Replies:
    6
    Views:
    537
    alex23
    Apr 21, 2010
  3. Henrik Faber
    Replies:
    8
    Views:
    403
    Ethan Furman
    Jul 26, 2012
  4. Ethan Furman

    Python 3: dict & dict.keys()

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

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

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    205
    Neil Cerutti
    Jul 24, 2013
  6. Ethan Furman

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

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    10
    Views:
    261
    Chris Angelico
    Jul 25, 2013
  7. Terry Reedy

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

    Terry Reedy, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    160
    Steven D'Aprano
    Jul 25, 2013
  8. Ethan Furman

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

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    7
    Views:
    209
    Peter Otten
    Jul 25, 2013
Loading...