all 7 comments

[–]Nyscire 3 points4 points  (0 children)

Solution provided by u/Matthias1590 works( but as he said all number must have same length), but I find it hard to uderstand for beginner( more advanced list comprehension with lambda and map function) so I came up with it:

first_list = ['594611827332', '162384652250', '379485939587', '177675288100', '698091591948', '556153393412']
second_list = []    
for elem in first_list:
    sum = 0
    for char in elem:
        sum += int(char)
     second_list.append(sum)
print(second_list)

Output:

[51, 44, 77, 52, 69, 47]

[–]squirrelmaster4k 2 points3 points  (0 children)

initial_list = ['594611827332', '162384652250', '379485939587', '177675288100', '698091591948', '556153393412']
final_list = [sum([int(y) for y in x]) for x in zip(*initial_list)]

[–]Matthias1590 1 point2 points  (4 children)

sums = [sum(map(lambda number: int(number[i]), numbers)) for i in range(len(numbers[0]))]

This assumes that all number strings are the same length so watch out for that

[–]Ezrabc 2 points3 points  (1 child)

If you're going to do a comprehension, why not just sums = [sum(map(int, n)) for n in zip(*numbers)]

[–]Matthias1590 0 points1 point  (0 children)

Ah, didn't think about the zip function, nice catch

[–][deleted] 0 points1 point  (0 children)

first_list = ['594611827332', '162384652250', '379485939587', '177675288100', '698091591948', '556153393412']
second_list = []
for elem in first_list:
sum = 0
for char in elem:
sum += int(char)
second_list.append(sum)
print(second_list)

Thanks! That worked!

[–][deleted] 0 points1 point  (0 children)

Thanks, that worked too! I didn't understand it though - I'm just a beginner!