0
import sys
su_pri=0
su_sc=0
n = int(raw_input().strip())
m=0

j=n-1
count=1
diff=0
a = []
for a_i in range(n):
    a_temp = map(int,raw_input().strip().split(' '))
    a.append(a_temp)
print a
while(m<=len(a)):
    su_pri=sum(su_pri,int(a[m]))
    m=m+n+1
while(count<=n):
    su_sc=su_sc+a[j]
    j=j+n-1
diff=abs(su_pri-su_sc)
print diff

if n=3 then the list is

3 11 2 4 4 5 6 10 8 -12 The list is [[11, 2, 4], [4, 5, 6], [10, 8, -12]] i want to store all elements into a single list with length=9(in this case) How can i do that??? Please tell me

m0nhawk
  • 22,980
  • 9
  • 45
  • 73

4 Answers4

1

Create a new list and add them together

newdiff = []
for eachlist in diff:
    newdiff += eachlist
print newdiff
Davidhall
  • 35
  • 11
0

For instance:

a = [[11, 2, 4], [4, 5, 6], [10, 8, -12]]
b = a[0] + a[1] + a[2]
stovfl
  • 14,998
  • 7
  • 24
  • 51
0

You want to add each element in the array to each other. Something like this maybe? Hope that helps :)

Community
  • 1
  • 1
Raults
  • 306
  • 2
  • 14
0

I think you're looking to try to flatten a list of lists in python, which I found this post really helpful.

l =  [[11, 2, 4], [4, 5, 6], [10, 8, -12]] #the list of lists to be flattened

flattenedList = [item for sublist in l for item in sublist]
Community
  • 1
  • 1