user8760022
user8760022

Reputation: 31

Sum the elements in a list that contains string and integer and put the answer in another list python

I have to write a code to sum the grades of each student in the list and return the total. My code is:

list=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]
list2=[]
for i in range(0,len(list1),4):
    list2.append(list1[i])
    for j in range(len(list1)):
        if j%4 == 1:
            sum= list1[j]+list1[j+1]+list1[j+2]
            list2.append(sum)
print(list2)

the expected output should be like:

['student1', 70, 'student2', 80,'student3', 90, 'student4', 60]

but i got this output:

['student1', 70, 80, 90, 60, 'student2', 70, 80, 90, 60, 'student3', 70, 80, 90, 60, 'student4', 70, 80, 90, 60]

so what is the wrong with my code?

Upvotes: 2

Views: 925

Answers (6)

Soner Say
Soner Say

Reputation: 136

it goes like this: 0, 4, 8, so you do not need the second for loop.

you already know where the numbers are.(i+1,i+2,i+3) and i is the student name.

list1=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]
list2=[]
for i in range(0, len(list1), 4):
    list2.append(list1[i])
    sum = list1[i+1]+list1[i+2]+list1[i+3]
    list2.append(sum)
print(list2)

Upvotes: 1

Alderven
Alderven

Reputation: 8260

You can do it with single loop:

lst = ['student1', 10, 20, 40, 'student2', 20, 20, 40, 'student3', 20, 30, 40, 'student4', 20, 10, 30]
result = []
for i in range(0, len(lst), 4):
    result.extend((lst[i], sum(lst[i+1:i+4])))

Output:

['student1', 70, 'student2', 80, 'student3', 90, 'student4', 60]

If number of marks are different from student to student, e.g.:

lst = ['student1', 10, 20, 'student2', 10, 20, 30, 'student3', 10, 20, 30, 40, 'student4', 10, 20, 30, 40, 50]

Then:

s = 0
result = [lst[0]]
for i in lst[1:]:
    try:
        s += int(i)
    except ValueError:
        result.extend((s, i))
        s = 0
result.append(s)

Output:

['student1', 30, 'student2', 60, 'student3', 100, 'student4', 150]

Upvotes: 1

sahasrara62
sahasrara62

Reputation: 11228

here is solution, where you dont't need to worry about the no of sbject score by student.

list1=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]

student_name =[]
student_name_index =[]

for i in range(len(list1)):        
    if type(list1[i]) == int:
        pass
    else:
        student_name.append(list1[i])
        student_name_index.append(i)

student_name_index.append(len(list1)-1)

total_marks=[]

for i in range(1,len(student_name_index)):
    total_marks.append(sum(list1[student_name_index[i-1]+1:student_name_index[i]]))

final_result =[]

for name, mark in zip(student_name,total_marks):
    final_result.append(name)
    final_result.append(mark)

print(final_result)

# output ['student1', 70, 'student2', 80, 'student3', 90, 'student4', 30] 

Upvotes: 0

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

list1=['student1',10,20,40,'student2',20,20,40,'student3',20,30,40,'student4',20,10,30]
list2=[]
for i in range(0,len(list1),4):
    list2.append(list1[i])
    sum= list1[i+1]+list1[i+2]+list1[i+3]
    list2.append(sum)
print(list2)

produces

['student1', 70, 'student2', 80, 'student3', 90, 'student4', 60]

The second for loop in code goes through every element in the list and take action whenever the index mod 4 is 1. You do not need two for loops.

Upvotes: 1

balderman
balderman

Reputation: 23815

Try

in_lst = ['student1', 10, 20, 40, 'student2', 20, 20, 40, 'student3', 20, 30, 40, 'student4', 20, 10, 30]
out_lst = []
for x in range(0, len(in_lst), 4):
    student_entry = in_lst[x:x + 4]
    out_lst.append(student_entry[0])
    out_lst.append(sum(student_entry[1:]))
print(out_lst)

Output

['student1', 70, 'student2', 80, 'student3', 90, 'student4', 60]

Upvotes: 0

trikPu
trikPu

Reputation: 162

In your second loop you loop again over your whole initial list1, thus you always append all the sums. If your list really always contains one name and three grades, you could instead get rid of the second loop and just use 'i' from your first loop as iterator for the sums, in the same manner as you are doing now.

Upvotes: 1

Related Questions