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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:48:32+00:00 2026-05-24T15:48:32+00:00

Considder the following interactive example >>> l=imap(str,xrange(1,4)) >>> list(l) [‘1’, ‘2’, ‘3’] >>> list(l)

  • 0

Considder the following interactive example

>>> l=imap(str,xrange(1,4))
>>> list(l)
['1', '2', '3']
>>> list(l)
[]

Does anyone know if there is already an implementation somewhere out there with a version of imap (and the other itertools functions) such that the second time list(l) is executed you get the same as the first. And I don’t want the regular map because building the entire output in memory can be a waste of memory if you use larger ranges.

I want something that basically does something like

class cmap:
    def __init__(self, function, *iterators):
        self._function = function
        self._iterators = iterators

    def __iter__(self):
        return itertools.imap(self._function, *self._iterators)

    def __len__(self):
        return min( map(len, self._iterators) )

But it would be a waste of time to do this manually for all itertools if someone already did this.

ps.
Do you think containers are more zen then iterators since for an iterator something like

for i in iterator:
    do something

implicitly empties the iterator while a container you explicitly need to remove elements.

  • 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-24T15:48:33+00:00Added an answer on May 24, 2026 at 3:48 pm

    You do not have to build such an object for each type of container. Basically, you have the following:

    mkimap = lambda: imap(str,xrange(1,4))
    list(mkimap())
    list(mkimap())
    

    Now you onlky need a nice wrapping object to prevent the “ugly” function calls. This could work this way:

    class MultiIter(object):
        def __init__(self, f, *a, **k):
            if a or k:
                self.create = lambda: f(*a, **k)
            else: # optimize
                self.create = f
        def __iter__(self):
            return self.create()
    
    l = MultiIter(lambda: imap(str, xrange(1,4)))
    # or
    l = MultiIter(imap, str, xrange(1,4))
    # or even
    @MultiIter
    def l():
        return imap(str, xrange(1,4))
    
    # and then
    print list(l)
    print list(l)
    

    (untested, hope it works, but you should get the idea)

    For your 2nd question: Iterators and containers both have their uses. You should take whatever best fits your needs.

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

Sidebar

Related Questions

Does anyone know or have good links that explain what iPhone's event loop does
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Let's consider following, simplified example: We have 2 tabs withing <rich:tabPanel switchType=ajax> , each
Consider following example: #include <iostream> #include <functional> #include <algorithm> #include <vector> #include <boost/bind.hpp> const
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider following code: char str[] = Hello\0; What is the length of str array,
Consider following example. #include <iostream> #include <boost/optional.hpp> template < typename A > int boo(

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.