web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Having Problem Run Pyt...
Power Automate
Answered

Having Problem Run Python script when variables defined external to script in PAD

(0) ShareShare
ReportReport
Posted on by 431

 

 

This script works with variables defined in the script.Steve48_0-1699307771586.png

 

This script works

--------------------------------------------------------

CriteriaList=[ "probable", "red" ]
MyList=["no help", "help", "not probable", "probable" ,  "red", "not red", "help me", "something different"]
CleanedList = []
for text in MyList:
    if not any(criteria in text for criteria in CriteriaList):
        CleanedList.append(text)
print(CleanedList)

 

output from working script

Steve48_4-1699308469591.png

 

 

I wanted to define variables externally in PAD but this script doesn't work.

---------------------------------------------------------------

CleanedList = []
for text in %MyList%:
    if not any(criteria in text for criteria in %CriteriaList%):
        CleanedList.append(text)
print(CleanedList)

Display Message is shown below. There is no script error

Steve48_1-1699308069321.png

 

Here is how the external variables are set

Steve48_2-1699308249466.png

Steve48_3-1699308311724.png

 

 

 

I have the same question (0)
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    This is because the list syntax in PAD is different from Python. Instead, try using Join text on your list in PAD to convert it into a string. Use ", " as the delimiter (double quotes, a comma and another double quotes).

    Then pass it into Python as follows:

    for text in ["%JoinedText%"]:

     

    This will result in Python interpreting it properly.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Steve48 Profile Picture
    431 on at

    Hi @Agnius 

    Still getting error. You can see updated flow and error in screenshot.

    Here is updated script:

    ----------------------------------

    CleanedList = []
    for text in ["%MyListJoined%"]:
        if not any(criteria in text for criteria in ["%CriteriaListJoined%"]):
            CleanedList.append(text)
    print(CleanedList)

    Thanks for response- Steve

     

    Steve48_0-1699366409506.png

     

  • Nived_Nambiar Profile Picture
    18,129 Super User 2025 Season 2 on at

    Hi @Steve48 

     

    Since you are passing joined string to the python script as input, then you have to split the same within python script to make things work 🙂

     

    see and try this modfied script :

     

    I am assuming that you have joined the string which is passed as input to python script is seperated by comma ,

    So i am splitting it comma as you see in split method in python script, you can change it accordingly to your need 🙂

     

     

     

    CleanedList = []
    for text in "%MyListJoined%".split(','):
     if not any(criteria in text for criteria in "%CriteriaListJoined%".split(',')):
     CleanedList.append(text)
    print(CleanedList)

     

     

    Let me know whether it works for you 🙂

     

    Thanks & Regards,

    Nived N 🚀

    LinkedIn: Nived N's LinkedIn
    YouTube: Nived N's YouTube Channel

    🔍 Found my answer helpful? Please consider marking it as the solution!
    Your appreciation keeps me motivated. Thank you! 🙌

  • Steve48 Profile Picture
    431 on at

    Hi @Nived_Nambiar @Agnius 

    Revised script but still getting error

    Steve48_1-1699385037315.png

     

     

     

     

    CleanedList = []
    for text in "%MyListJoined%".split(','):
     if not any(criteria in text for criteria in "%CriteriaListJoined%".split(',')):
     CleanedList.append(text)
    print(CleanedList)

     

     

    Hi @Nived_Nambiar @Agnius Updated script but still getting error as shown in screenshot:

     

     

     

     

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    If you do it with .split() in Python, avoid using double quotes in the delimiter in Join text. Just join them over a comma and then use a comma to split it in Python. The double quotes now make Python read "[" as a string and then it encounters the word no outside of quotes, which causes it to break.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    P.S. If this is the actual script you intend to use, I'm not quite sure why you want to do it in Python. You're still building nested loops in there and you can do exactly the same (without really losing much in terms of efficiency) within native actions in PAD.

  • Steve48 Profile Picture
    431 on at

    Hi @AG, cc @Nived_Nambiar I implemented your (Agnius') suggestion but still getting error. This is not actual script. It is a test script so I could set the basic approach for actual code. The reason I am attempting to do in Python is that PAD was painfully slow. I thought Python would be faster. Is this true? Below are screenshots that show my latest code.

    Steve48_1-1699447337222.png

    CleanedList = []
    for text in "%MyListJoined%".split(','):
     if not any(criteria in text for criteria in "%CriteriaListJoined%".split(',')):
     CleanedList.append(text)
    print(CleanedList)

     

    Steve48_2-1699450746197.png

     

     

     

  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    When you're creating the lists using Set variable, you need to encapsulate the entire string in %%. What you're doing right now is not actually creating a list, but a string instead. It should look like %["probable","red","help"]%

     

    Alternatively, since you need it as a string anyway, you could also avoid creating it as a list and then using Join text. Simply use Set variable, but remove the square brackets and the quotation marks. Simply leave the text values separated by a comma.

     

    Then send that to the Python script and use .split() on it.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Steve48 Profile Picture
    431 on at

    Hi @Agnius That worked! Thank you that was a big help. When I'm doing my actual flows, when I run into a problem, often I create a separate test flow to simulate the problem. In this case my actual flow involved iterating a list. That's why I used lists instead of strings in this example. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard