0

I am new to python coding, I would like some help in creating nested list with specific place values. I want to list values which are in 0,3,6,9... into one nested list, 1,4,7,10 in one nested list and so on.

I have an original list as:

 List1 = [A,B,C,D,E,F,G,H,I,J,K,L]

I need output nested list like this:

 List2 = [[A,D,G,J],[B,E,H,K],[C,F,I,L]]

Currently I am using python 2.7 version. I am not bothered about sorting the list before or after.Can anyone please help me with python code for this?

Explore_SDN
  • 217
  • 1
  • 3
  • 10

1 Answers1

1

You could use a list comprehension with slicing:

>>> List1 = list('ABCDEFGHIJKL')
>>> List1
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
>>> [List1[i::3] for i in range(3)]
[['A', 'D', 'G', 'J'], ['B', 'E', 'H', 'K'], ['C', 'F', 'I', 'L']]
Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677