How to print every third element from current index in list of elements

shanky

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
>>> 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']]
>>> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to multiply every third element of a list together if the first elements are the same?

How to remove every third element from a linked list recursively?

How to discard every third element in a list?

How to print the element in a list with index?

How to traverse loop in Go and print out every third element

How to print the sum of the current and previous element in a list

How can I search if en element from one List exists in the elements in the other list and then print this element

How do I mutate every third element in a list?

How to remove every third element in a php array until only one element remains and print that element?

How to get current index of element in polars list

How to print an index of element in a list - python

Javascript | How to add an element to an array every n elements starting from index x?

how to put every third element from an array map into a div react

How to get every element from 1-15 into a list from a vector that has over 800 elements, in R

How to print both the index and value for every element in a Vec?

How to print every nth index of a python list on a new line?

How to print every third character of a string? java

I want to print the pair of elements of a list starting from 1 index

How to remove current element from List in java

How to specify index of specific elements in every sublist of nested list?

Remove every third element from array?

How to alphabetize list elements between every occurrence of specific element?

How to list every possible parent elements of a child element in xmlstarlet

How do I increment every element in an array list by n elements?

How to print out every other element from String in Kotlin

How to select every third element with jquery?

How to delete elements from a List synchronously by Index?

How to print all elements after a specific element in a list?

How to print a list with each element's index? - Python