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 7896341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:52:17+00:00 2026-06-03T07:52:17+00:00

Is there a way to efficiently iterate over the values/items in a dictionary that

  • 0

Is there a way to efficiently iterate over the values/items in a dictionary that works in both Python 2 and Python 3?

In Python 2, I can write

for x in mydict:
for x in mydict.iterkeys():
for x in mydict.viewkeys():
for x in mydict.itervalues():
for x in mydict.viewvalues():
for x in mydict.iteritems():
for x in mydict.viewitems():

and in Python 3, I have the following possibilities:

for x in mydict:
for x in mydict.keys():
for x in mydict.values():
for x in mydict.items():

So, I can iterate over the keys with for x in mydict, and that works in both Python 2 and 3. But is there a way to iterate over values and key-value pairs (‘items’) that works universally? I need it for writing Python code that can be run in both Python versions.

(On a side note, other obstacles with iterators can be bypassed easily; take for example:

if sys.version_info.major<3:
    from itertools import izip as zip, imap as map

However, the dictionary methods cannot be redefined easily like map and zip.)

Any ideas?

  • 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-03T07:52:19+00:00Added an answer on June 3, 2026 at 7:52 am

    values() version of Just another dunce’s answer

    for value in (mydict[key] for key in mydict):
    

    or

    def dict_values(d):
       return (mydict[key] for key in mydict)
    
    def dict_items(d):
       return ((key, mydict[key]) for key in mydict)
    
    for value in dict_values(mydict):
        ...
    
    for value in dict_items(mydict):
        ...
    

    This is pretty crappy though. You could check the python version and instead of returning the generator, return d.items() or d.iteritems() as appropriate

    I think a better question may be how to write Python2 code that works well with 2to3 and generates good Python3 code

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

Sidebar

Related Questions

I'd like to write a Python or Java app to iterate over all of
Is there a way to compute efficiently the Fourier transform of the max of
I'm wondering if there's a super-efficient way of confirming that an Image object references
I know I can iterate over a map m with for k, v :=
Is there an efficient way to version store procedures written in PL/SQL? (I only
Is there an efficient way to clone an object yet leave out specified properties?
Is there are more efficient way than the following for selecting the third parent?
Is there a fast/efficiency way to check if a table is empty? DECLARE @StartEndTimes
Is there any way we could do efficient paging with petapoco and mvc3. At
I'm curious if there is an efficient way to wait for the front page

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.