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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:49:29+00:00 2026-05-18T02:49:29+00:00

Is there a generic way of getting an iterator that always iterates over the

  • 0

Is there a generic way of getting an iterator that always iterates over the values (preferably, although it may work iterating the keys too) of either dictionaries or other iterables (lists, sets…)?

Let me elaborate: When you execute “iter(list)” you get an iterator over the values (not the indexes, which sound pretty similar to the “key” in the dictionary) but when you do “iter(dict)” you get the keys.

Is there an instruction, attribute… whatever… which always iterates over the values (or the keys) of an iterable, no matter what type of iterable it is?

I have a code with an “append” method that needs to accept several different types of iterable types, and the only solution I’ve been able to come up with is something like this:

#!/usr/bin/python2.4

class Sample(object):
    def __init__(self):
        self._myListOfStuff = list()

    def addThings(self, thingsToAdd):
        myIterator = None
        if (isinstance(thingsToAdd, list) or 
            isinstance(thingsToAdd, set) or 
            isinstance(thingsToAdd, tuple)):
                myIterator = iter(thingsToAdd)
            elif isinstance(thingsToAdd, dict):
                myIterator = thingsToAdd.itervalues()

        if myIterator:
            for element in myIterator:
                self._myListOfStuff.append(element)


if __name__ == '__main__':
    sample = Sample()
    myList = list([1,2])
    mySet = set([3,4])
    myTuple = tuple([5,6])
    myDict = dict(
        a= 7,
        b= 8
    )

    sample.addThings(myList)    
    sample.addThings(mySet) 
    sample.addThings(myTuple)   
    sample.addThings(myDict)
    print sample._myListOfStuff
    #Outputs [1, 2, 3, 4, 5, 6, 7, 8]

I don’t know… It looks a little bit… clunky to me

It would be great to be able to get a common iterator for cases like this, so I could write something like…

def addThings(self, thingsToAdd):
    for element in iter(thingsToAdd):
        self._myListOfStuff.append(element)

… if the iterator always gave the values, or…

def addThings(self, thingsToAdd):
    for element in iter(thingsToAdd):
        self._myListOfStuff.append(thingsToAdd[element])

… if the iterator returned the keys (I know the concept of key in a set is kind of “special”, that’s why I would prefer iterating over the values but still… maybe it could return the hash of the stored value).

Is there such thing in Python? (I have to use Python2.4, by the way)

Thank you all in advance

  • 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-05-18T02:49:29+00:00Added an answer on May 18, 2026 at 2:49 am

    Most collections have no concept or “keys” or “values”. They just store items, and iterating over them gives you those items. Only mappings (dict-likes) provide “keys” and “values”. And the default way of iterating them uses the keys because for x in collection: assert x in collection should be an invariant (and a (key, value) in d rarely makes sense, as opposed to key in d). So there is no method to iterate over the values only, because the least collections have things called “values”.

    You can, however, choose to ignore the keys in mappings. Since you have to use Python 2.4, using ABCs is out of questions… I fear the simplest way would be:

    def iter_values(it):
        if hasattr(it, 'itervalues'):
            return it.itervalues()
        return iter(x)
    

    Which can still break, but handles dicts and similar mappings.

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

Sidebar

Related Questions

Is there a way to make reflector disassemble back to the new c# constructs?
So I'm getting started learning Rails. Now that Rails 3 is out, I want
I'd like to instantiate a model (in Zend Framework) using a generic way. I
I'm getting a cannot convert from 'int' to 'TValue' in the following code. How
Given a generic array T[] , where T extends java.lang.Number , I would like
Is there a query in SQL Server 2008 which will delete data from all
in my unit test framework, for some of the messages ( which are simply
I'm following on from a previous question . The answer I accepted involves using
Updated According to comments: I have outlog.txt file which contains multiple filenames, e.g: 2345_535_Dell&HP_3PAR_DEAL.txt
I'd like to have a custom function repeatedly called as long as a custom

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.