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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:53:56+00:00 2026-06-16T01:53:56+00:00

Consider the following example code. The example is given just to highlight the different

  • 0

Consider the following example code. The example is given just to highlight the different functionality between map and itertools.imap. What I really want to do cannot be
done with a list comprehension because in my real problem I am not creating a list but filling a larger numpy array with small arrays. So in response to the code below, please do not suggest: [f(x) for x in range(3)]

Example:

g = []

def f(x):
    g.append(x)

The results I get with map and itertools.map:

map(f, range(3)) # Results in g = [0,1,2]

itertools.imap(f, range(3)) # Does not change g

I want the g to change as done by the map function.
However, I have heard that map will (or does) behave like
itertools.imap in Python 3. Even though I am using Python 2.7 I want to learn the correct way of using an iterator version of the map. How do I use itertools.imap to achieve
the same outcome as I get with map?

I can do:

b = itertools.imap(f, range(3))
list(b) # This gives g = [0,1,2] if starting with an empty g.

Is this the correct way, or is there a better way?

Thanks 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-06-16T01:53:57+00:00Added an answer on June 16, 2026 at 1:53 am

    itertools functions return generators; they only operate when iterated over. So itertools.imap(f, range(3)) won’t actually do anything until you run it to completion e.g. with list.

    Per http://docs.python.org/2/library/itertools.html#recipes, the most efficient way to consume an iterator is with a zero-length deque:

    collections.deque(itertools.imap(f, range(3)), maxlen=0)
    

    However, if you’re calling a function for its side effects you should be using an imperative as opposed to functional syntax:

    for i in range(3):
        f(i)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
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?
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following code example (python 2.7): class Parent: def __init__(self, child): self.child =
Consider the following example F# code: type mytype() = member this.v = new OtherClass()
Consider the following code example: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} --
Consider the following code: (live example here ) $(function() { var wrapper = $(<div
Consider following example code: #include <iostream> #include <inttypes.h> using namespace std; int f(uint32_t i)
Consider the following example program: next :: Int -> Int next i | 0
Consider the following example. I have an interface MyInterface, and then two abstract classes

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.