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 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 generic way of determining all attributes (and their values) from an
Is there a way to declare that the variable type of a generic class
There is any way to set the generic type (T) of class in the
Is there any way to store the generic parameter type passed in at construction
Is there a way to make this method generic so I can return a
Is there a way in Boo to express some constaints on generic types as
I was wondering if there's a way I could code some kind of generic
Let's imagine I have instantiated generic type by Activator.CreateInstance. Is there any way to
Is there any generic alternative / implementation for MemoryCache? I know that a MemoryCache
I'm building an application that supports plugins over a generic type of data. Problem

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.