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)  
3
  • Related to How do you split a list into evenly sized chunks? Commented Jul 12, 2017 at 0:57
  • 1
    Your requested output is not every third element, it's every fourth (4 increments between A and E for example) Commented Jul 12, 2017 at 0:58
  • @Evert Ah, I believe you're right. I'll modify my comment saying it's related. Commented Jul 12, 2017 at 1:00

2 Answers 2

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']]
>>> 
Sign up to request clarification or add additional context in comments.

18 Comments

And now with your edit my answer is useless hahah, jokes aside, as always good readable answer providing num_of_parts variable.
@ritesht93 can we do the same operation using mod operator?
@ViníciusAguiar hehe yeah
@shanky added another method that uses the mod operator
@shanky made the mod operator method more readable/understandable
|
3

A one-liner solution is:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.