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

The Archive Base Latest Questions

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

I have: dictionary = {foo:12, bar:2, jim:4, bob: 17} I want to iterate over

  • 0

I have:

dictionary = {"foo":12, "bar":2, "jim":4, "bob": 17}

I want to iterate over this dictionary, but over the values instead of the keys, so I can use the values in another function.

For example, I want to test which dictionary values are greater than 6, and then store their keys in a list. My code looks like this:

list = []
for c in dictionary:
    if c > 6:
        list.append(dictionary[c])
print list

and then, in a perfect world, list would feature all the keys whose value is greater than 6.
However, my for loop is only iterating over the keys; I would like to change that to the values!

Any help is greatly appreciated.
thank you

  • 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-03T08:43:18+00:00Added an answer on June 3, 2026 at 8:43 am
    >>> d = {"foo": 12, "bar": 2, "jim": 4, "bob": 17}
    >>> [k for k, v in d.items() if v > 6] # Use d.iteritems() on python 2.x
    ['bob', 'foo']
    

    I’d like to just update this answer to also showcase the solution by @glarrain which I find myself tending to use nowadays.

    [k for k in d if d[k] > 6]
    

    This is completely cross compatible and doesn’t require a confusing change from .iteritems (.iteritems avoids saving a list to memory on Python 2 which is fixed in Python 3) to .items.

    @Prof.Falken mentioned a solution to this problem

    from six import iteritems
    

    which effectively fixes the cross compatibility issues BUT requires you to download the package six

    However I would not fully agree with @glarrain that this solution is more readable, that is up for debate and maybe just a personal preference even though Python is supposed to have only 1 way to do it. In my opinion it depends on the situation (eg. you may have a long dictionary name you don’t want to type twice or you want to give the values a more readable name or some other reason)

    Some interesting timings:

    In Python 2, the 2nd solution is faster, in Python 3 they are almost exactly equal in raw speed.


    $ python -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k, v in d.items() if v > 6]'
    1000000 loops, best of 3: 0.772 usec per loop
    $ python -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k, v in d.iteritems() if v > 6]'
    1000000 loops, best of 3: 0.508 usec per loop
    $ python -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k in d if d[k] > 6]'
    1000000 loops, best of 3: 0.45 usec per loop
    
    $ python3 -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k, v in d.items() if v > 6]'
    1000000 loops, best of 3: 1.02 usec per loop
    $ python3 -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k in d if d[k] > 6]'
    1000000 loops, best of 3: 1.02 usec per loop
    

    However these are only tests for small dictionaries, in huge dictionaries I’m pretty sure that not having a dictionary key lookup (d[k]) would make .items much faster.
    And this seems to be the case

    $ python -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k in d if d[k] > 6]'
    1 loops, best of 3: 1.75 sec per loop
    $ python -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k, v in d.iteritems() if v > 6]'
    1 loops, best of 3: 1.71 sec per loop
    $ python3 -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k in d if d[k] > 6]'
    1 loops, best of 3: 3.08 sec per loop
    $ python3 -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k, v in d.items() if v > 6]'
    1 loops, best of 3: 2.47 sec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Dictionary called AvailableBoxes. I want to lookup a string in this
I have a dictionary: D = { foo : bar, baz : bip }
suppose I have a dictionary whose keys are strings. How can I efficiently make
Say you have, foo = 'bar' d = {'a-key':'a-value'} And you want d =
I have two iterables, and I want to go over them in pairs: foo
If I have a dictionary with the following key/value foo/bar and declared [dictionary setObject:@baz
I have a dictionary of the type: IDictionary<foo, IEnumerable<bar>> my_dictionary bar class looks like
I have obj like this {hello: 'world', foo.0.bar: v1, foo.0.name: v2, foo.1.bar: v3} It
I have a list of variable names, like this: ['foo', 'bar', 'baz'] (I originally
I have an object Dictionary<int, Dictionary<int, Foo> > on one side, and a function

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.