Friday, 15 April 2011

Python list arithmetic -



Python list arithmetic -

i have list of integers example: [30,21,32,32,41,20,21,32,21,20] have variable of x, x has value within range of list. how can find sum of elements in list number of x. illustration if x 4 want: 30+21+32+32

if want sum x first elements:

>>> l = [1,2,3,4,5,6,7,8,9] >>> x = 5 >>> result = sum(l[:x]) >>> result 15

my reply may not efficient, straightforward.

python list

No comments:

Post a Comment