Result of Python script is returned without changing list items in if-statement

Thanks in advance for your help.

I started with some Python programming for elaborating Python nodes in Dynamo.

I tried to create a list and adapt the list items by using a PhytonScript. If i use a custom list at IN[3] the script doesn´t add a praefix neither adds a value to the list items (it works correctly if i don´t change the input list).

First approach (ok)
case11

Second approach with custom list (not like expected)
case22

I already tried to reproduce the problem in a python online compiler, but in the online compiler the expected result get displayed (?). Link to SO question:

python - Result of function is returned without changing list items in if-statement - Stack Overflow

Dynamo-file with PythonScript:

PythonScripting.dyn (18.7 KB)

> listing = [0,1,2,3,4,5,None,"null", "", "Text"]
> praefix = IN[1]
> add = IN[2]
> 
> if IN[3]:
>     listing = IN[4]
> 
> if IN[0]:
>     for x in range(len(listing)):
>         if isinstance(listing[x], int):
>             listing[x] = praefix + str(listing[x] + add)
>         
>     OUT = listing
>     
> else:
>     
>     for x in range(len(listing)):
>         listing[x] = listing[x] + add
> 
>     OUT = listing
> ```

For some reason python is not recognizing the integers in your custom list. I added this at the beginning to convert them.

listing = [0,1,2,3,4,5,None,"null", "", "Text"]
praefix = IN[1]
add = IN[2]
custom = IN[4]
 
if IN[3]:
    for a in range(len(custom)):
    	try:
    		listing[a] = int(custom[a])
    	except:
    		pass
1 Like

Thank you very much! Maybe the int values are not recognized, because i mixed it with str.

Full code of the Python node:

listing = [0,1,2,3,4,5,None,"null", "", "Text"]

praefix = IN[1]
add = IN[2]
custom = IN[4]

newListing = []

if IN[3]:
	listing = custom
	
if IN[3]:
	for x in range(len(custom)):
			try:
				listing[x] = int(custom[x])
			except:
				pass

if IN[0]:
	for x in range(len(listing)):
	    if isinstance(listing[x], int):
			listing[x] = praefix + str(listing[x] + add)
		
	newListing = listing
	
else:
	
	for x in range(len(listing)):
		listing[x] = listing[x] + add

	newListing = listing

OUT = newListing

Hello

Because input values are Net type (Int64)
image

another way to fix

import System
listing = [0,1,2,3,4,5,None,"null", "", "Text"]
praefix = IN[1]
add = IN[2]

if IN[3]:
	listing = IN[4]

if IN[0]:
	for x in range(len(listing)):
	    if isinstance(listing[x], (int, System.Int16, System.Int32, System.Int64)):
			listing[x] = praefix + str(listing[x] + add)
		
	OUT = listing
	
else:
	
	for x in range(len(listing)):
		listing[x] = listing[x] + add

	OUT = listing
2 Likes