0

I want to change the values of a list without using a loop or increase the performence

Below is a part of my code.I want to add (1*2),(2*3) ..to a certain range range of the list.Is there any quicker way to do this other than using for loop

    k=1
    for j in range(3,100):
        ary[j-1]+=k*(k+1)
        k+=1
5
  • 2
    Can you clarify what exactly you're intending? What's the start to end range supposed to be? Are you looking to create a list, add values to an existing one or change values in an existing one? Commented Jun 24, 2015 at 13:45
  • 1
    Do you mean "quicker" performance-wise or as "with less lines of code"? Commented Jun 24, 2015 at 13:45
  • 1
    use recursion to do it without a loop Commented Jun 24, 2015 at 13:47
  • 1
    change values on existing one Commented Jun 24, 2015 at 13:47
  • @LeartS performmence wise Commented Jun 24, 2015 at 13:48

2 Answers 2

1

If you mean using list comprehension try this:

a = [j * (j + 1) for j in xrange(1, 100)]

It will return a list: [(1*2),(2*3),(3*4),(4*5),...]

Sign up to request clarification or add additional context in comments.

Comments

0

You can use recursive method but isn't be faster than your current code.

I think there isn't better way to modify specific range of values that not change uniformly like that.

1 Comment

Using list comprehension is only syntax, performance is the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.