Change language

# Python | Print list items in a circle

|

Examples :

`  Input:  list = [1, 2, 3, 4, 5, 6, 7], k = 3, n = 10  Output:  [1, 2, 3] [4, 5, 6] [7, 1, 2] [3, 4, 5] [6, 7, 1] [2, 3, 4] [5, 6, 7] [1, 2, 3] [4, 5, 6] [7, 1, 2]  Input:  list = [10, 20, 30, 40, 50, 60, 70], k = 4, n = 5  Output:  [10, 20, 30, 40] [50, 60, 70, 10] [20, 30, 40, 50] [60, 70, 10, 20] [30, 40, 50, 60] `

We can use ` itertools ` with ` zip ` to do this tasks.

Example # 1:

 ` # Python code to print an element in a group ` ` # 5 to 9 iterations in a circular range. `   ` # Import ` ` from ` ` itertools ` ` import ` ` cycle ` ` `  ` # list initialization ` ` List ` ` = ` ` [` ` 90 ` `, ` ` 99 ` `, ` ` 192 ` `, ` ` 0 ` `, ` ` 43 ` `, ` ` 55 ` `] `   ` # Determine the number of iterations ` ` n ` ` = ` ` 9 `   ` # Definition of no grouping ` ` k ` ` = ` 5   ` for ` ` index, ` ` * ` ` ans ` ` in ` ` zip ` ` (` ` range ` ` (n), ` ` * ` ` [cycle (` ` List ` `)] ` ` * ` ` k): ` ` # print responses ` ` print ` ` (ans) `

Exit:

` [90, 99, 192, 0, 43] [55, 90, 99, 192, 0] [43, 55, 90, 99, 192] [0, 43, 55, 90, 99] [192, 0, 43, 55, 90] [99, 192, 0, 43, 55] [90, 99, 192, 0, 43] [55, 90 , 99, 192, 0] [43 , 55, 90, 99, 192] `

Example # 2:

` `

` # Python print code element in the group # 6 to 4 iterations in a circular range.   # Import from itertools import cycle   # list initialization List = [ ’Geeks’ , ’for’ , ’ geeks’ , ’is’ , ’ portal’ ]   # Determine the number of iterations n = 4   # Definition no grouping k = 6    for index, * ans in zip ( range (n ), * [cycle ( List )] * k): # print responses print (ans) `

` ` Exit:

` [’Geeks’,’ for ’,’ geeks’, ’is’,’ portal’, ’Geeks’] [’ for’, ’geeks’,’ is’, ’portal’,’ Geeks’, ’for’] [’ geeks’, ’is ’,’ portal’, ’Geeks’,’ for’, ’geeks’] [’ is’, ’portal’,’ Geeks’, ’for’,’ geeks’, ’is’] `

## Shop

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Best laptop for Zoom

\$499

Best laptop for Minecraft

\$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method