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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:35:22+00:00 2026-05-13T14:35:22+00:00

Is there a more concise, efficient or simply pythonic way to do the following?

  • 0

Is there a more concise, efficient or simply pythonic way to do the following?

def product(lst):
    p = 1
    for i in lst:
        p *= i
    return p

After some tests I have found out that this is marginally faster than using operator.mul:

from operator import mul
# from functools import reduce # python3 compatibility

def with_lambda(lst):
    reduce(lambda x, y: x * y, lst)

def without_lambda(lst):
    reduce(mul, lst)

def forloop(lst):
    r = 1
    for x in lst:
        r *= x
    return r

import timeit

a = range(50)
b = range(1,50)#no zero
t = timeit.Timer("with_lambda(a)", "from __main__ import with_lambda,a")
print("with lambda:", t.timeit())
t = timeit.Timer("without_lambda(a)", "from __main__ import without_lambda,a")
print("without lambda:", t.timeit())
t = timeit.Timer("forloop(a)", "from __main__ import forloop,a")
print("for loop:", t.timeit())

t = timeit.Timer("with_lambda(b)", "from __main__ import with_lambda,b")
print("with lambda (no 0):", t.timeit())
t = timeit.Timer("without_lambda(b)", "from __main__ import without_lambda,b")
print("without lambda (no 0):", t.timeit())
t = timeit.Timer("forloop(b)", "from __main__ import forloop,b")
print("for loop (no 0):", t.timeit())

gives me

('with lambda:', 17.755449056625366)
('without lambda:', 8.2084708213806152)
('for loop:', 7.4836349487304688)
('with lambda (no 0):', 22.570688009262085)
('without lambda (no 0):', 12.472226858139038)
('for loop (no 0):', 11.04065990447998)
  • 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-13T14:35:23+00:00Added an answer on May 13, 2026 at 2:35 pm

    Without using lambda:

    from operator import mul
    # from functools import reduce # python3 compatibility
    reduce(mul, list, 1)
    

    it is better and faster. With python 2.7.5

    from operator import mul
    import numpy as np
    import numexpr as ne
    # from functools import reduce # python3 compatibility
    
    a = range(1, 101)
    %timeit reduce(lambda x, y: x * y, a)   # (1)
    %timeit reduce(mul, a)                  # (2)
    %timeit np.prod(a)                      # (3)
    %timeit ne.evaluate("prod(a)")          # (4)
    

    In the following configuration:

    a = range(1, 101)  # A
    a = np.array(a)    # B
    a = np.arange(1, 1e4, dtype=int) #C
    a = np.arange(1, 1e5, dtype=float) #D
    

    Results with python 2.7.5

    
           |     1     |     2     |     3     |     4     |
    -------+-----------+-----------+-----------+-----------+
     A       20.8 µs     13.3 µs     22.6 µs     39.6 µs     
     B        106 µs     95.3 µs     5.92 µs     26.1 µs
     C       4.34 ms     3.51 ms     16.7 µs     38.9 µs
     D       46.6 ms     38.5 ms      180 µs      216 µs
    

    Result: np.prod is the fastest one, if you use np.array as data structure (18x for small array, 250x for large array)

    with python 3.3.2:

    
           |     1     |     2     |     3     |     4     |
    -------+-----------+-----------+-----------+-----------+
     A       23.6 µs     12.3 µs     68.6 µs     84.9 µs     
     B        133 µs      107 µs     7.42 µs     27.5 µs
     C       4.79 ms     3.74 ms     18.6 µs     40.9 µs
     D       48.4 ms     36.8 ms      187 µs      214 µs
    

    Is python 3 slower?

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

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer i ^= 1; XOR the value with 1. This gives… May 13, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer You can get a hold of the application's stdout by… May 13, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer I dont know how to do it in the technologies… May 13, 2026 at 11:19 pm

Related Questions

I am constructing a query that will naturally return a result that has values
Yes, There's More Than One Way To Do It but there must be a
Alright, I have some data that I need to assign an int type identifier
What is the most concise and efficient way to find out if a JavaScript
Is there a more concise/standard idiom (e.g., a JDK method) for piping an input

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.