# Indexing variables in a list comprehension

Suppose I create the polynomial ring R = PolynomialRing(QQ, ['lambda%s'%i for i in [1 .. g]] + ['psi%s'%i for i in [1 .. n]]).

If I want to create a list comprehension which creates a list of perhaps all the lambdas, what is the notation used at the beginning of the list comprehension?

i.e. [lambdai for i in [1 .. g]].

edit retag close merge delete

Sort by » oldest newest most voted You can use R.gen(i-1), but it takes fewer keystrokes to slice the list of generators:

sage: g = 3; n = 4
sage: R = PolynomialRing(QQ, ['lambda%s'%i for i in [1 .. g]] + ['psi%s'%i for i in [1 .. n]])
sage: lambdas = R.gens()[:g]
sage: psis = R.gens()[n-1:]
sage: lambdas, psis
((lambda1, lambda2, lambda3), (psi1, psi2, psi3, psi4))


Another trick is to start your indexing at 0 so that you can name your list like psi and then psi will refer to the variable named psi0. Or you can shift your list, or make a dictionary, if you want indices to start at 1.

more

1

Unfortunately you can't name your list lambda.