winpriv
winpriv

Reputation: 21

How to print the first and the second values in array the same time in " for loop " python

is it possible to print the first value and the second value in a loop - before the loop ends ?

here's what im trying to do

My code :

from time import sleep
import pyautogui

strtxt = [
"Testing1"  , 
"Testing2", 
"Testing3",
"Testing4",
"Testing5",
"Testing6",
"Testing7",
"Testing8",
]




for x in strtxt:

    sleep(1)
    pyautogui.write(x) # write the str in the array
    pyautogui.press ('tab') 

output/

# Writes it like a nomral loop 

Testing1 Testing2 .. ..

What i need it to do:

i need it to print/write 

Testing1 Testing2 sleeps then Testing3 Testing4 sleeps ...

Upvotes: 0

Views: 142

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

You can zip the list against itself with different striding, the first through even elements and the second through odd elements.

for i,j in zip(strtxt[::2], strtxt[1::2]):
    print('{} {} ...'.format(i,j))

Output

Testing1 Testing2 ...
Testing3 Testing4 ...
Testing5 Testing6 ...
Testing7 Testing8 ...

Upvotes: 3

Related Questions