How can I pick up certain elements from a list and construct a nested list?

How can I pick up certain elements from a list and construct a nested list?

For example:

Original list : [0, 1, 2 ,3, 4, 5, 6, 7, 8]

and the list I want:

[ [0, 3, 6], [1, 4, 7], [2, 5 ,8]

I was trying to use for loop, but some how, I kept failing.

Could someone plz help me with this?
Thanks guys!

It’s hard to say what you’re doing wrong in your loop without seeing your code, but here is one way to solve your problem:

alist = [0, 1, 2 ,3, 4, 5, 6, 7, 8]
blist = [[], [], []]

for x in alist:
    if x % 3 == 0:
        blist[0].append(x)
    elif x % 3 == 1:
        blist[1].append(x)
    else:
        blist[2].append(x)

Here is a more compact solution:

alist = [0, 1, 2 ,3, 4, 5, 6, 7, 8]
blist = [[], [], []]

for x in alist:
    blist[x % 3].append(x)

Hope that helps!

Thank you so much!!

[[i for i in range(9) if i % 3 == j] for j in range(3)]