Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8346177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:55:30+00:00 2026-06-09T06:55:30+00:00

After many attempts to create one-liners that will invert key-value pairs, and reverse an

  • 0

After many attempts to create one-liners that will invert key-value pairs, and reverse an OrderedDict, I have this:

    from collections import OrderedDict as OD

    attributes=OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')))
    print(attributes)

    reversed_attributes=OD(reversed(list(attributes.items())))
    print(reversed_attributes)

    inverted_attributes=OD([reversed(item) for item in attributes.items()])
    print(inverted_attributes)

    ''' Prints 
        OrderedDict([('brand', 'asus'), ('os', 'linux'), ('processor', 'i5'), ('memory', '4G')])
        OrderedDict([('memory', '4G'), ('processor', 'i5'), ('os', 'linux'), ('brand', 'asus')])
        OrderedDict([('asus', 'brand'), ('linux', 'os'), ('i5', 'processor'), ('4G', 'memory')])
    '''

This works, but is it inefficient? By using reversed(list(a.items())) is this creating a lot of overhead, and so is not pythonic? Same for the inverted_attributes.

The point was to avoid for loops and so on, but will this decrease performance as we scale up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:55:31+00:00Added an answer on June 9, 2026 at 6:55 am

    interesting I also came up with other ways.

    >>> from collections import OrderedDict as OD
    >>> attributes = OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')))
    

    if you want to reverse you can do this

    >>> reverse = OD(attributes.items()[::-1])
    

    or a more pythonic approach:

    >>> reverse = OD(reversed(attributes.items()))
    

    note you don’t need to create list items is already a list, and while reversed is a generator OrderedDict will simply iterate through to it generate the new dict.

    both generating similar timings.

    $ python -m timeit "from collections import OrderedDict as OD; attributes = OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')))" "reverse = OD(attributes.items()[::-1])"
    10000 loops, best of 3: 54.8 usec per loop
    $ python -m timeit "from collections import OrderedDict as OD; attributes = OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')))" "reverse = OD(reversed(attributes.items()))"
    10000 loops, best of 3: 54.4 usec per loop
    $ python -m timeit "from collections import OrderedDict as OD; attributes = OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')))" "reversed_attributes=OD(reversed(list(attributes.items())))"
    10000 loops, best of 3: 54.4 usec per loop
    

    if you want to invert:

    >>> invert = OD(zip(*zip(*attributes.items())[::-1]))
    

    or more pythonic:

    >>> invert = OD(map(reversed, attributes.items()))
    

    again both generating similar timings.

    $ python -m timeit "from collections import OrderedDict as OD; attributes = OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')));" "invert = OD(zip(*zip(*attributes.items())[::-1]))"
    10000 loops, best of 3: 57 usec per loop
    $ python -m timeit "from collections import OrderedDict as OD; attributes = OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')));" "invert = OD(map(reversed, attributes.items()))"
    10000 loops, best of 3: 56.8 usec per loop
    $ python -m timeit "from collections import OrderedDict as OD; attributes = OD((('brand','asus'), ('os','linux'), ('processor','i5'), ('memory','4G')));" "inverted_attributes=OD([reversed(item) for item in attributes.items()])"
    10000 loops, best of 3: 55.8 usec per loop
    

    you can use the two methods in conjunction to both reverse and invert.

    This works, but is it inefficient? By using reversed(list(a.items())) is this creating a lot of overhead, and so is not pythonic? Same for the inverted_attributes.

    something can generate a lot of overhead and be pythonic on the other hand something can very very efficient and not be very pythonic, the term has being a bit abused, but thats just my opinion

    exert from wikipedia:

    A common neologism in the Python community is pythonic, which can have a wide range of meanings related to program style. To say that code is pythonic is to say that it uses Python idioms well, that it is natural or shows fluency in the language. Likewise, to say of an interface or language feature that it is pythonic is to say that it works well with Python idioms, that its use meshes well with the rest of the language.

    In contrast, a mark of unpythonic code is that it attempts to write C++ (or Lisp, Perl, or Java) code in Python—that is, provides a rough transcription rather than an idiomatic translation of forms from another language. The concept of pythonicity is tightly bound to Python’s minimalist philosophy of readability and avoiding the “there’s more than one way to do it” approach. Unreadable code or incomprehensible idioms are unpythonic.

    as for:

    but will this decrease performance as we scale up?

    This hard to say, without knowing why you are making such transforms, or whether or not their an integral part of your system, fundamentally at a bare minimun they are adding linear time/space overhead which may or may not be good, if the number of entries remains small, then no problem but if at every request, assuming this happening at a web server, your are doing this on a large dicts, this can be quite harsh, and may require a redesign to avoid this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have refereed old questions and found that people face many issues after installing
After many hair-pulling frustrations I've finally got one version of the PerlMagick module working
I have what I know is a simple question, but after many searches in
After one week searching and converting many algorithm from other language into php to
After many and many attempts I can't get the Phonegap + phonegap-plugin-facebook-connect build in
Continuation of: Standalone Cross Platform (Windows/Linux)) File Compression for C/C++? After many attempts on
I feel like this one should be easy but after numerous searches and attempts
After many attempts to crack this I am stuck so I turn to SO
In a C# winforms program, I have an event that fires when someone attempts
I have two tables, Table_A and Table_B. Table_A has a one-to-many relationship with Table_B.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.