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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:34:17+00:00 2026-06-03T08:34:17+00:00

I have wrote following code for a simple problem in Python – def Peu1(numbers):

  • 0

I have wrote following code for a simple problem in Python –

def Peu1(numbers):
    "Sum of all the multiples of 3 or 5 below 1000."
    for num in range(numbers):
        if num%3 == 0 or num%5 == 0:
            test = sum(range(numbers),0)
        return test

print Peu1(1000)

I want to change it to List comprehension, I wrote following –

test = [sum(range(numbers),0) for num in range(numbers) if num%3 == 0 or num%5 == 0]
print test

but my list comprehension is printing result in a loop, I mean I am getting result n times(mod 3 or 5). Please pinpoint the mistake and guide.

  • 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-03T08:34:19+00:00Added an answer on June 3, 2026 at 8:34 am

    A list comprehension is designed to build up a list – if you are not doing that, then you don’t need to use one.

    You could, however, use a list comprehension or generator expression inside your function, to generate the list of numbers to use:

    sum(num for num in range(numbers) if num%3 == 0 or num%5 == 0)
    

    Note that numbers is a bit of a misleading variable name – it would imply to me that it contained the range of numbers, I’d suggest either passing in the range of numbers, or calling it highest or something.

    Rather than using the or with the same value, some people find something like if 0 in {num%3, num%5} clearer, personally, I think in this case it obscures the meaning a little.

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

Sidebar

Related Questions

I have an interesting problem. I wrote the following perl script to recursively loop
I have problem installing simple Windows Service, my code looks like this: using System;
I have a problem with this very simple block of code. please give me
I have the following simple code, that reads contents of a text file into
I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write
I wrote a simple Tkinter based Python application that reads text from a serial
I have the following very simple XML file and I want to quickly parse
I have some python code for Google App Engine that responds with the string
i have a simple problem with that fetch_array. i like to read out x1
I have the following problem. I have a data set that has the beginning

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.