Change language

Python | Join a cycle in a list

|

Method: Using yield + loop + generator
A crude method to execute, — this is to use a generator and keep printing the value if we know the elements are bound to form a loop, and this is done in an infinite loop and stops when no more matches are found.

# Python3 code to demonstrate how it works
# Join a loop in the list
# Using yield + loop + generator

 
# helper function for this task

def cycle (test_list, val, stop = None ):

temp = dict (test_list)

stop = stop if stop is not None else val

while True :

yield (val)

val = temp.get (val, stop)

if val = = st op: break

 
# initializing list

test_list = [ [ 6 , 7 ], [ 9 , 6 ], [ 7 , 9 ]]

 
# print of the original list

print ( "The original list is: " + str (test_list))

 
# Join the loop in the list
# Using yield + loop + generator
# print result

print ( " The cycle elements are: " )

for ele in cycle (test_list, 6 ):

print (ele)

Output:

 The original list is: [[6, 7], [9, 6], [7, 9]] The cycle elements are: 6 7 9 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

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