Parth Srivastava
Parth Srivastava

Reputation: 3

I want to split the sublists into its elements

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

Upvotes: 0

Views: 70

Answers (4)

user3554661
user3554661

Reputation: 32

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]

Upvotes: 0

Raults
Raults

Reputation: 306

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

Upvotes: 0

stovfl
stovfl

Reputation: 15513

For instance:

a = [[11, 2, 4], [4, 5, 6], [10, 8, -12]]
b = a[0] + a[1] + a[2]

Upvotes: 0

Davidhall
Davidhall

Reputation: 35

Create a new list and add them together

newdiff = []
for eachlist in diff:
    newdiff += eachlist
print newdiff

Upvotes: 1

Related Questions