### Thread: How to return list of lists

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0

#### How to return list of lists

Hi

My understanding of python is very basic and I was wondering how I can return this table as a list of lists.

1 2 3 4 5
3 4 5 6
5 6 7
7 8
9

The function is
def Table(n):
Then how would I make it that when the value of n is 4 it returns [[1, 2, 3], [3, 4], [5]].

Thank you
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
138
Rep Power
6
Originally Posted by Bobdy
Hi

My understanding of python is very basic and I was wondering how I can return this table as a list of lists.

1 2 3 4 5
3 4 5 6
5 6 7
7 8
9

The function is
def Table(n):
Then how would I make it that when the value of n is 4 it returns [[1, 2, 3], [3, 4], [5]].

Thank you
Are you in the same class as "mrhlth" ?

http://forums.devshed.com/python-programming-11/listoflists-944040.html
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0
Originally Posted by partoj
Are you in the same class as "mrhlth" ?
yeah probably lol, i've read that thread but still dont have a clue what to do
4. In that case, just reuse the mrhlth function then delete all the zeros.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0
the mrhlth function?
6. Use the relevant function in the other thread.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0
Originally Posted by b49P23TIvg
Use the relevant function in the other thread.
do you mean this one?

Code:
```def f(n):
'''
whacko array creator
>>> f(4) == [[1,2,3],[0,3,4],[0,0,5]]
True
'''
A = [[0 for i in range(n - 1)] for j in range(n - 1)]
A[0][:] = range(1, n) # first row  ******1
for i in range(1, n - 1): # (yes, this range would need to change as well to answer the second question)
for j in range(i, n):
A[i][j] = A[i-1][j] + 1  # ******2
return A```
I tried using this one but I keep getting 0s returned :s