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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:31:40+00:00 2026-06-09T00:31:40+00:00

I need to do this in Python. There is a given list l,may contain

  • 0

I need to do this in Python.
There is a given list l,may contain more than 5000 integer elements.
There is a limit on sum of the numbers,20000 or may be high.
The output should be all the possible sums of 2 numbers picked from list,
Like,

l=[1,2,3,4,5,6,7,8,9]
output 
1+1,1+2,1+3,1+4,1+5,1+6...........
2+2,2+3,2+4.......
.........
.......

2,3,4,5,6... like that

I’m using this code,Doing this for now,
But It’s slow

l=listgen()
p=[]
for i in range(0,len(l)):
    for j in range(i,len(l)):
        k=l[i]+l[j]
        if k not in p:
            p.append(k)
p.sort
print(p)

listgen() is the function that generating the input list.

  • 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-09T00:31:42+00:00Added an answer on June 9, 2026 at 12:31 am

    Some old-fashioned optimization might get you faster code that’s easier to grok than list comprehensions with multiple for loops:

    def sums(lst, limit):    # prevent global lookups by using a function
        res = set()          # set membership testing is much faster than lists
        res_add = res.add    # cache add method
        for i, first in enumerate(lst):   # get index and item at the same time
            for second in lst[i:]:        # one copy operation saves n index ops.
                res_add(first + second)   # prevent creation/lookup of extra local temporary
        return sorted([x for x in res if x < limit])
    
    print sums(listgen(), 20000)
    

    as an added bonus, this version will optimize beautifully with psyco, cython, etc.

    Update:
    When comparing this to the other suggestions (replacing listgen with range(5000), I get:

    mine:        1.30 secs
    WolframH:    2.65 secs
    lazyr:       1.54 secs (estimate based on OPs timings -- I don't have Python 2.7 handy)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been playing with Python and I have this list that I need worked
how good this python code ? need criticism) there is a error in this
I am trying to convert this to python. I just really need help with
I'm currently in Python land. This is what I need to do. I have
I need this for some animation effects and i remember there is a window
This may seem like the worlds simplest python question... But I'm going to give
I need to determine if a given Python variable is an instance of native
In sum: I need to write a List Comprehension in which i refer to
I find myself often wanting to write Python list comprehensions like this: nearbyPoints =
I have a list of many Python objects like this: class RangeClass(object): def __init__(self,address,size):

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.