List structure in Python

Hi all,

I am just trying basic python to find prime numbers between a range (lower and upper). But I couldn’t quite get the output in the form of list levels… I want the output lists (prime nums, count, sum) to be nested lists of the same lengths.

lower = IN[0]
upper = IN[1]
prime_num = []

for l,u in zip(lower, upper):
	for num in range(l, u+1):
		if num > 1:
			for i in range(2, num-1):
				if num % i == 0:
					break
			else:
				prime_num.append(num)
	
OUT = prime_num, len(prime_num), sum(prime_num)

@theshysnail ,

actually it was not solved… maybe you can load the packages

1 Like

Thanks for linking the topic. Here I am just focusing on trying to manage my output lists into sublists instead of getting prime numbers.

After struggling a while, I was able to achieve it.

def tolist(input):
	result = input if isinstance(input, list) else [input]
	return result

lower = tolist(IN[0])
upper = tolist(IN[1])
prime_list = []

for l,u in zip(lower, upper):
	prime_num = []
	for num in range(l, u+1):	
		if num > 1:	
			for i in range(2, num-1):
				if num % i == 0:
					break
			else:
				prime_num.append(num)
	prime_list.append(prime_num)

p_count = []
p_sum = []

for i in prime_list:
	p_count.append(len(i))
	p_sum.append(sum(i))
	
OUT = prime_list, p_count, p_sum
1 Like