2

I have list of elements lst = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']. How to print every third elements from current index in following format? [[ 'A', 'E', 'I'],[ 'B', 'F', 'J'],[ 'C', 'G'],[ 'D', 'H']]:

def printFormat(lst, columns):
  for i in range(0, len(lst)):
    print lst[i]
    for j in range():
    #if (i)%3==0:
    #    print lst[i]


if __name__ == '__main__':
    lst = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
    columns = 3
    printFormat(lst, columns)  
riteshtch
  • 8,629
  • 4
  • 25
  • 38
shanky
  • 751
  • 1
  • 16
  • 46

2 Answers2

3
>>> lst
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
>>> num_of_parts=4
>>> newlst=[lst[i::4] for i in range(num_of_parts)]
>>> newlst
[['A', 'E', 'I'], ['B', 'F', 'J'], ['C', 'G'], ['D', 'H']]
>>> 

Edit 1 : adding some explanation and 2nd method that uses the mod operator

list_obj[start:stop:step] is similar to the method slice(start, stop, step). start indicates from where slicing must start and the slicing stops at stop - 1. If start and stop are ignored, they are replaced by 0 and len(list) - 1 respectively.

Hence start and stop are meant to get a sublist/slice of the list and step gets every step (every 2nd/3rd etc) element from that sublist/slice.

Here are some examples demonstrating the same:

>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[1:3]
[1, 2]
>>> lst[1:]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[:3]
[0, 1, 2]
>>> lst[::2]
[0, 2, 4, 6, 8]
>>> lst[::1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[1::2]
[1, 3, 5, 7, 9]
>>> 

Mod operator method:

>>> lst
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
>>> num_of_parts=4
>>> newlst=[[lst[i] for i in range(len(lst)) if i%4 == j] for j in range(num_of_parts)]
>>> newlst
[['A', 'E', 'I'], ['B', 'F', 'J'], ['C', 'G'], ['D', 'H']]
>>> 

Edit 2 : Making the mod operator method more readable we get the following:

>>> lst=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
>>> newlst=[]
>>> for j in range(num_of_parts):
...     temp=[]
...     for i in range(len(lst)):
...             if i%4 == j:
...                     temp.append(lst[i])
...     newlst.append(temp)
... 
>>> newlst
[['A', 'E', 'I'], ['B', 'F', 'J'], ['C', 'G'], ['D', 'H']]
>>> 
riteshtch
  • 8,629
  • 4
  • 25
  • 38
  • And now with your edit my answer is useless hahah, jokes aside, as always good readable answer providing `num_of_parts` variable. – Vinícius Figueiredo Jul 12 '17 at 01:06
  • @ritesht93 can we do the same operation using mod operator? – shanky Jul 12 '17 at 01:14
  • Can you or someone please explain also how the `::` syntax works? To someone who's never seen this before, it looks confusing. I'd prefer a full explanation rather than magic code. – darksky Jul 12 '17 at 01:32
  • 1
    @ViníciusAguiar hehe yeah – riteshtch Jul 12 '17 at 01:36
  • @shanky yes it is doable with mod operator too, let me come up with something – riteshtch Jul 12 '17 at 01:36
  • @darksky it is similar to the `slice` method which takes 3 parameters: start, stop and step. From the docs: https://docs.python.org/3/library/functions.html#slice – riteshtch Jul 12 '17 at 01:38
  • @shanky Thanks. Naturally, `number_of_parts` here should be `len(lst)`. – darksky Jul 12 '17 at 01:39
  • 1
    @shanky added another method that uses the mod operator – riteshtch Jul 12 '17 at 01:44
  • 1
    @shanky made the mod operator method more readable/understandable – riteshtch Jul 12 '17 at 01:57
  • @ritesht93 The number of sublists we want is equal to the `len(lst)` because there are exactly that many elements in the list to start a sublist from. – darksky Jul 12 '17 at 02:04
  • @darksky no, if we put `num_of_parts` as equal to `len(lst)` then the result will have initial values as the answer but there will be more sublists added into the result which is not what is desired. – riteshtch Jul 12 '17 at 02:07
  • @darksky `[lst[i::4] for i in range(len(lst))]` gives the output as `[['A', 'E', 'I'], ['B', 'F', 'J'], ['C', 'G'], ['D', 'H'], ['E', 'I'], ['F', 'J'], ['G'], ['H'], ['I'], ['J']]`. Notice that initial 4 elements correspond to our answer but there are more sublist added too. And if you see the number of sublist then that is equal to the number of elements present in the list i.e 10 – riteshtch Jul 12 '17 at 02:08
  • Exactly. Unless OP needs only the first four. – darksky Jul 12 '17 at 02:12
  • Have a look at the code OP wrote up there: `for i in range(0, len(lst)):`. I think this might indicate he/she might be interested in them all. – darksky Jul 12 '17 at 02:13
  • @darksky yes. and also I noticed that the question had been edited wrongly missing the ending brackets in the output. Corrected the question. Ohh and regarding the loop you mentioned, OP used that because he wanted to code the solution using `mod` operator – riteshtch Jul 12 '17 at 02:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148949/discussion-between-darksky-and-ritesht93). – darksky Jul 12 '17 at 02:15
  • @darksky anyways I think OP accepted the answer and Thanks for pointing the different result as well :) – riteshtch Jul 12 '17 at 02:20
  • Sure. I think It was ambiguous precisely because the ending bracket was missing :) – darksky Jul 12 '17 at 02:23
3

A one-liner solution is:

>>> [lst[i::4] for i in range(4)]
[['A', 'E', 'I'], ['B', 'F', 'J'], ['C', 'G'], ['D', 'H']]
Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44