# Creating list within an outer for loop counter

Hello,

I want to create one list for L. I gave an example from the list tutorial(list1) that does what I want to do. I believe I have to include the for loop inside the brackets [] but cannot figure it out. I do not know how to program in Python. I am learning C++ now and hope to learn Python next.

for i in range (1,100,1):
div = divisors(i)
length = len(div)
L = [sum(div[k] for k in range(length))-i]
print i,

print L

list1 =[j for j in range(6)]
print list1


Thanks

edit retag close merge delete

Please describe in words, mathematically what you want.

It may be you want

[ sigma(k) for k in range(1..99) ]


Just google for python list comprehension to see how lists can be defined "almost mathematically".

Sort by » oldest newest most voted

Hello,

I finally figured out how to create the above in one list. Instead of multiple rows with one element, I have one row with multiple elements. I don't know if that is the right "descriptive language" in Sage or python talk so just copy and paste, compare to above and you will see what I mean.

L1 =[divisors(i) for i in range(1,100,1)]
print L1

L2 =[sum(divisors(i))-i for i in range(1,100,1)]
print L2

more

OK, so you wanted [ sigma(k)-k for k in range(1,100) ] , since using the L2 from the above code...

sage: L2 == [ sigma(k)-k for k in range(1,100) ]
True


(The sigma function is already summing the divisors, per default taken at first power. Please realize that from the post it is hard to know which list was desired. Even having the answer, this is not fully clear, for instance - do we need also the list L1? What for? Why do we need L2? Note that structurally the sum of all divisors is the more structural function, since it is algebraically multiplicative, for instance sigma(32) * sigma(49) == sigma( 32*49 ) delivers True...)