sage

Defining a list of lists in SageMath


In SageMath, I want to define a list whose elements are lists with two elements.

I tried to do it by appending elements to the inner lists, accessed by their index in the outer list:

K=GF(3)

InitialStateSet=[]

c=0
for i in K:
    for j in K:
        if (i,j)!=(0,0):
            InitialStateSet[c].append(i)
            InitialStateSet[c].append(j)
        c+=1
print(InitialStateSet)

Solution

  • Does this do what you want?

    sage: K=GF(3)
    sage: InitialStateSet=[]
    sage: for i in K:
    ....:     for j in K:
    ....:         if (i,j)!=(0,0):
    ....:             InitialStateSet.append([i,j])
    ....: 
    sage: InitialStateSet
    [[0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
    

    Or somewhat more compactly:

    sage: K=GF(3)
    sage: InitialStateSet=[[i,j] for i in K for j in K]