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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:01:27+00:00 2026-06-07T07:01:27+00:00

map and filter are often interchangeable with list comprehensions, but reduce is not so

  • 0

map and filter are often interchangeable with list comprehensions, but reduce is not so easily swapped out as map and filter (and besides, in some cases I still prefer the functional syntax anyway). When you need to operate on the arguments themselves, though, I find myself going through syntactical gymnastics and eventually have to write entire functions to maintain readability.

I’ll use map to keep the illustration unit-test simple, but please keep in mind that real-life use-cases might be harder to express as a list comprehension.

I’ve found two messy ways to go about it, but nothing I would ever actually use.

[afunc(*i) for i in aniter] == map(afunc, *zip(*aniter))
[afunc(*i) for i in aniter] == map(lambda i: apply(afunc, i), aniter)

Is there any pithy, elegant way to express the right hand side of these expressions?

  • 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-07T07:01:29+00:00Added an answer on June 7, 2026 at 7:01 am

    Check out itertools for tools that will make your life easier.

    For example, the code you posted is already available as itertools.starmap.

    itertools.starmap(afunc, aniter)
    

    From the documentation:

    Make an iterator that computes the function using arguments obtained from the iterable. Used instead of imap() when argument parameters are already grouped in tuples from a single iterable (the data has been “pre-zipped”). The difference between imap() and starmap() parallels the distinction between function(a,b) and function(*c). Equivalent to:

    def starmap(function, iterable):
       # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
       for args in iterable:
           yield function(*args)
    

    There are also tons over other goodies hidden in itertools, so I recommend you read through the documentation to see if there is anything else there that you can use. The recipes section also show ways that you can use the functions available in itertools to solve a variety of problems. Even if you can’t find a recipe that solves your exact requirements, it’s likely that you can use some of the ideas demonstrated as a source of inspiration.

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

Sidebar

Related Questions

When should you use map/filter instead of a list comprehension or generator expression?
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
(define filter-in (lambda (predicate list) (let((f (lambda (l) (filter-in-sexpr predicate l)))) (map f list))))
I'm writing an ISAPI filter for the IIS7 that should map some functionalities of
Is it possible to combine Underscore's filter and map? I currently have two separate
I have a TreeMap resMap new TreeMap<String, Map<String, String>>(); I would like to filter
Map<String, List<String>> words = new HashMap<String, List<String>>(); List<Map> listOfHash = new ArrayList<Map>(); for (int
Map Reduce is a pattern that seems to get a lot of traction lately
map.root :controller => main, :action => index is not redirecting the main controller to
I find myself often writing code following the pattern: foo xs = map snd

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.