list within a list
Posted
by
atm atm
on Stack Overflow
See other posts from Stack Overflow
or by atm atm
Published on 2012-10-02T03:24:09Z
Indexed on
2012/10/02
3:37 UTC
Read the original article
Hit count: 136
python
|python-3.x
I'm working on this problem, but I cannot figure out the second part. I tried using reverse list but it did not work out how I planned it.
Given a list L (e.g. [1,2,3,4]), write a program that generates the following nested lists:
- L1 =
[[1],[1,2],[1,2,3],[1,2,3,4]]
,- L2 =
[[4],[3,4],[2,3,4],[1,2,3,4]]
.
My code that I have so far:
mylist=[,1,2,3,4]
print("Orginal list L=",mylist)
n=len(mylist)
l1=[]
l2=[]
for x in range(1,n+1,1):
l1.append(mylist[0:x])
print("L1=",l1) #prints final product of l1
mylist.reverse() #this is where i get messed up
for x in range(1,n+1,1):
l2.append(mylist[0:x])
print("L2=",l2)
© Stack Overflow or respective owner