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

  • Home
  • SEARCH
  • 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 8576575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:57:18+00:00 2026-06-11T19:57:18+00:00

Is iterating over some_dict.items() as efficient as iterating over a list of the same

  • 0

Is iterating over some_dict.items() as efficient as iterating over a list of the same items in CPython?

  • 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-11T19:57:19+00:00Added an answer on June 11, 2026 at 7:57 pm

    It depends on which version of Python you’re using. In Python 2, some_dict.items() creates a new list, which takes up some additional time and uses up additional memory. On the other hand, once the list is created, it’s a list, and so should have identical performance characteristics after the overhead of list creation is complete.

    In Python 3, some_dict.items() creates a view object instead of a list, and I anticipate that creating and iterating over items() would be faster than in Python 2, since nothing has to be copied. But I also anticipate that iterating over an already-created view would be a bit slower than iterating over an already-created list, because dictionary data is stored somewhat sparsely, and I believe there’s no good way for python to avoid iterating over every bin in the dictionary — even the empty ones.

    In Python 2, some timings confirm my intuitions:

    >>> some_dict = dict(zip(xrange(1000), reversed(xrange(1000))))
    >>> some_list = zip(xrange(1000), xrange(1000))
    >>> %timeit for t in some_list: t
    10000 loops, best of 3: 25.6 us per loop
    >>> %timeit for t in some_dict.items(): t
    10000 loops, best of 3: 57.3 us per loop
    

    Iterating over the items is roughly twice as slow. Using iteritems is a tad bit faster…

    >>> %timeit for t in some_dict.iteritems(): t
    10000 loops, best of 3: 41.3 us per loop
    

    But iterating over the list itself is basically the same as iterating over any other list:

    >>> some_dict_list = some_dict.items()
    >>> %timeit for t in some_dict_list: t
    10000 loops, best of 3: 26.1 us per loop
    

    Python 3 can create and iterate over items faster than Python 2 can (compare to 57.3 us above):

    >>> some_dict = dict(zip(range(1000), reversed(range(1000))))
    >>> %timeit for t in some_dict.items(): t      
    10000 loops, best of 3: 33.4 us per loop 
    

    But the time to create a view is negligable; it is actually slower to iterate over than a list.

    >>> some_list = list(zip(range(1000), reversed(range(1000))))
    >>> some_dict_view = some_dict.items()
    >>> %timeit for t in some_list: t
    10000 loops, best of 3: 18.6 us per loop
    >>> %timeit for t in some_dict_view: t
    10000 loops, best of 3: 33.3 us per loop
    

    This means that in Python 3, if you want to iterate many times over the items in a dictionary, and performance is critical, you can get a 30% speedup by caching the view as a list.

    >>> some_list = list(some_dict_view)
    >>> %timeit for t in some_list: t
    100000 loops, best of 3: 18.6 us per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a pythonic way of iterating over first n items of an
I'm working with django, and I'm iterating over a list of different keys, Now
I am iterating over a list of objects of mixed types. For each object
I am iterating over a list of objects. With each iteration I build and
Following is a method that is iterating over a list: @Override public void DataRowMapper(List<?>
When iterating over a standard container, do you think it's a good idea to
I'm iterating over a JRE Collection which enforces the fail-fast iterator concept, and thus
I want avoid iterating over a nil array. My bad solution: if nil!=myArr myArr.each
I have the following issue related to iterating over an associative array of strings
Is there a better shorter way than iterating over the array? int[] arr =

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.