True for any [Python]

Hi All,

I’m trying to return any true values in nested lists, can anyone help?

"
data = IN[0]
OUT, OUT1 = [], []
for x in data:
	for b in x:
		sublist = []
		if b == True:
			sublist.append(True)
		else:
			sublist.append(False)
	OUT.append(sublist[0])


";

Hi, you should try:

data = IN[0]
OUT, OUT1 = [], []
for x in data:
	for b in x:
		sublist = []
		if b:
			sublist.append(True)
		else:
			sublist.append(False)
	OUT.append(sublist[0])

Still doesn’t work i’m afraid

1 Like

I appreciate that this works but my example shows i’m having difficulty with nested lists.

list = IN[0]
out = []
for sublist in list:
	if True in sublist:
		out.append(True)
	else:
		out.append(False)
OUT = out
5 Likes

I know that this question is answered and your code works but just for the sake of it here are two notes about your Python code.

  1. It’s not good practice to overwrite python’s inbuilt types. Do not name a variable as list.
  2. List comprehension is probably a more elegant solution for this case:
OUT = [True if True in sublist else False for sublist in IN[0]]
8 Likes