Manu
Manu

Reputation: 31

Python List Comprehension cross sum /total

I tried to write a listcomprehension, which forms the cross-sum of all numbers between 0 and 1000.

[ x for x in range(1001) sum([int(y) for y in str(x)])]

I would like to have a list containing:

[0,1,2,3,4,5,6,7,8,9,1,2,3,4 .... 27,1]

Upvotes: 0

Views: 628

Answers (1)

John Gordon
John Gordon

Reputation: 33345

You nearly had it:

[sum(int(y) for y in str(x)) for x in range(1001)]

Upvotes: 2

Related Questions