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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:07:03+00:00 2026-05-23T20:07:03+00:00

I would like to iterate over the outputs of an unknown function. Unfortunately I

  • 0

I would like to iterate over the outputs of an unknown function. Unfortunately I do not know whether the function returns a single item or a tuple. This must be a standard problem and there must be a standard way of dealing with this — what I have now is quite ugly.

x = UnknownFunction()
if islist(x):
    iterator = x
else:
    iterator = [x]

def islist(s):
    try:
        len(s)
        return True
    except TypeError:
        return False

for ii in iterator:
    #do stuff
  • 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-23T20:07:04+00:00Added an answer on May 23, 2026 at 8:07 pm

    The most general solution to this problem is to use isinstance with the abstract base class collections.Iterable.

    import collections
    
    def get_iterable(x):
        if isinstance(x, collections.Iterable):
            return x
        else:
            return (x,)
    

    You might also want to test for basestring as well, as Kindall suggests.

        if isinstance(x, collections.Iterable) and not isinstance(x, basestring):
    

    Now some people might think, as I once did, “isn’t isinstance considered harmful? Doesn’t it lock you into using one kind of type? Wouldn’t using hasattr(x, '__iter__') be better?”

    The answer is: not when it comes to abstract base classes. In fact, you can define your own class with an __iter__ method and it will be recognized as an instance of collections.Iterable, even if you do not subclass collections.Iterable. This works because collections.Iterable defines a __subclasshook__ that determines whether a type passed to it is an Iterable by whatever definition it implements.

    >>> class MyIter(object):
    ...     def __iter__(self):
    ...         return iter(range(10))
    ... 
    >>> i = MyIter()
    >>> isinstance(i, collections.Iterable)
    True
    >>> collections.Iterable.__subclasshook__(type(i))
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to iterate over a collection and at the same time also
i would like to iterate over the items of a List<T> , except the
I would like to iterate. @some_value outputs the following result: { Meta => {
In DataSet.Tables[0].Columns[0] we have a DataType property. Now, I would like to iterate over
I would like to pass arbitrary container as an argument of function and iterate
I have two arrays of objects. I would like iterate over each of these
I have a Map[Long, String] which I would like iterate over in descending order
I would like to iterate over a Set and remove the elements from the
I am using Ruby on Rails 3.0.7 and I would like to iterate over
Given a text file with multiple lines, I would like to iterate over each

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.