python - How to switch between loops? -


i have program generates 2 lists. want print item list1 switch printing item list 2 , go printing list1 ..etc. whenever try it prints list1 list2.

please help.

code:

list1 = ['a', 'b' , 'c', 'd', 'e', 'f']  list2 = ['1', '2', '3', '4', '5', '6']  continue = true  while continue == true:     in list1:         print         print '/n'         continue = false  while continue == false:      in list2:           print           print '/n'           continue = true 

output:

a b c d e f 1 2 3 4 5 6 

desired output:

a 1 b 2 c 3 d 4 e 5 f 6 

my answer based around code question. if format wanting use answer. otherwise, other answers more pythonic stated.

please note i've renamed "continue" "switch" continue reserved python word, producing syntax error.

list1 = ['a', 'b' , 'c', 'd', 'e', 'f']  list2 = ['1', '2', '3', '4', '5', '6']  switch = true while true:     while switch == true:         in list1:             print(i)             list1.pop(0)             switch = false             break      while switch == false:         in list2:             print(i)             list2.pop(0)             switch = true             break 

if set state of variable switch break loop desire.

due loop break .pop() 0th index value ensure correct output received.

this code inefficient , sure can find other methods of producing desired output.

edit: unequal list lengths must add switch = false @ end of while switch == true: loop , vice versa while switch == false:

edit 2: gives solution switching between loops :)


Comments