Loop through multiple lists simultaneously?

Question:

I am looking for some help with loops. I have a name list, and I’m looking to sequentially message each profile from that list with a random user inputted message.

Ideally this would involve looping through the profileLink items while simultaneously looping through the nameList items so that I can build a new messageInput for each profile.

My code works, but currently has a lot of duplication and to build this to support a larger list will involve a lot of copying and pasting, as you can see below – How would you go about this?

messageInput0="Hi " + f'{namelist[0]}' + ", " + f'{choice(messageOptions)}'
messageInput1="Hi " + f'{namelist[1]}' + ", " + f'{choice(messageOptions)}'
messageInput2="Hi " + f'{namelist[2]}' + ", " + f'{choice(messageOptions)}'
messageInput3="Hi " + f'{namelist[3]}' + ", " + f'{choice(messageOptions)}'
messageInput4="Hi " + f'{namelist[4]}' + ", " + f'{choice(messageOptions)}'

webbrowser.open(profileLink[0])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput0, interval=random.uniform(0.03, 0.15))

with pyautogui.hold('command'):
    pyautogui.press('w')

webbrowser.open(profileLink[1])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput1, interval=random.uniform(0.03, 0.15))

with pyautogui.hold('command'):
    pyautogui.press('w')

webbrowser.open(profileLink[2])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput2, interval=random.uniform(0.03, 0.15))

with pyautogui.hold('command'):
    pyautogui.press('w')

    webbrowser.open(profileLink[3])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput3, interval=random.uniform(0.03, 0.15))

with pyautogui.hold('command'):
    pyautogui.press('w')

    webbrowser.open(profileLink[4])
time.sleep(6)
pyautogui.press('tab')
pyautogui.write(messageInput4, interval=random.uniform(0.03, 0.15))

with pyautogui.hold('command'):
    pyautogui.press('w')

EDIT: After trying the top answer, I got the error that list object has no attribute ‘replace’

Worth mentioning my list is built using str.replace/str.split functions, like so:

profileLink = open("profiles.txt").read().splitlines()

profileLink = [item.replace("+","name=") for item in profileLink]
newLinks = [item.replace("%","name=") for item in profileLink]
nameList = [i.split("name=")[1] for i in newLinks]

EDIT 2: First edit was actually an unrelated error. Fixed now.

Asked By: dan.rad

||

Answers:

Use a for loop:

for i in range(len(profileLink)):
    webbrowser.open(profileLink[i])
    time.sleep(6)
    pyautogui.press('tab')
    message = f'Hi {namelist[i]}, {choice(messageOptions)}'
    pyautogui.write(message, interval=random.uniform(0.03, 0.15))
Answered By: Mathias R. Jessen

Like so.

  • Use zip to "zip up" the name and profile link and you don’t have to keep track of loop indexes.
import random
import time
import pyautogui
import webbrowser

namelist = ["a", "b", "c", "d"]
profilelink = ["q", "w", "e", "r"]
messageOptions = ["bye", "hello"]

for name, link in zip(namelist, profilelink):
    message = f"Hi {name}, {random.choice(messageOptions)}"

    webbrowser.open(link)
    time.sleep(6)
    pyautogui.press("tab")
    pyautogui.write(message, interval=random.uniform(0.03, 0.15))

    with pyautogui.hold("command"):
        pyautogui.press("w")
Answered By: AKX

This is how I’d do it:

import random
import time
import webbrowser


def messageInput(nameList, profileLink):
    for name in nameList:
        message = "Hi " + f'{name}' + ", " + f'{choice(messageOptions)}'
        for profile in profileLink:
            webbrowser.open(profile)
            time.sleep(6)
            pyautogui.press('tab')
            pyautogui.write(message, interval=random.uniform(0.03, 0.15))

        with pyautogui.hold('command'):
            pyautogui.press('w')
Answered By: MoroccanTea
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.