0

I have a for loop with some calculations stored in a list. During second iteration of for loop, the earlier list is replaced with new one. I want to add new list to corresponding entries of old list:

for row in file:
    theValue = row.getValue("Value")

    if row.getValue("Value") == 20:
        ..................................
        a = ..............................
    b = [x*float(theValue) for x in a]

How to add "b" list into the list obtained during the second iteration in above for loop? For example:

b(1) = [2,3,4]
b(2) = [3,5,1]

so I want to get:

b = [5,8,5]
4
  • Do you want to add elements to the list or the list itself? And what do you mean by to corresponding entries of old list? Commented Oct 13, 2013 at 6:40
  • 1
    This question is somewhat confusing. Can you add an example? Commented Oct 13, 2013 at 6:46
  • @vidit: I have added an example to illustrate it. Commented Oct 13, 2013 at 6:48
  • I have updated my answer accordingly. Commented Oct 13, 2013 at 6:49

1 Answer 1

1
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
>>> a = [1, 2, 3]
>>> b = [0] * len(a)
>>> b
[0, 0, 0]
>>> for the_value in [4, 5]:
...     for i, x in enumerate(a):
...         b[i] += x * float(the_value)
... 
>>> b
[9.0, 18.0, 27.0]
>>> 
Sign up to request clarification or add additional context in comments.

Comments

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.