hi I am a beginner python user studying python is fun and i have learned many thing now a days I am working on a program to find prime numbers and non prime number in a list generated by range function.

i am done with my work the problem is that now i want to print both lists the prime numbers and non prime numbers vertically having 4 columns each and side by side i figured out how to print in four columns but how do i print both list side by side.

if anyone can help i will be very thank ful.

Recommended Answers

All 3 Replies

So you want something like this (formatting not to scale!):

Prime                       Non-prime
2    3   5   7              1   4  6   8
11 13  17 19             9 10 12 14

?

yes i want to print like this

Here's one possibility:

def get_four(mylist, index):
	retval = ""
	for i in range(index, min(index+4, len(mylist))):
		retval += str(mylist[i])+"\t"
	return retval

def print_two_lists(list1, list2):
	for i in range(0, max(len(list1), len(list2)), 4):
		print get_four(list1,i),
		if get_four(list1,i):
			print "\t", get_four(list2,i)
		else:
			print "\t\t\t\t\t", get_four(list2,i)

>>> l1 = range(20)
>>> l2 = range(30,70,2)
>>> 
>>> print_two_lists(l1,l2)
0	1	2	3		30	32	34	36	
4	5	6	7		38	40	42	44	
8	9	10	11		46	48	50	52	
12	13	14	15		54	56	58	60	
16	17	18	19		62	64	66	68	

>>> l3 = range(30,70)
>>> print_two_lists(l1,l3)
0	1	2	3		30	31	32	33	
4	5	6	7		34	35	36	37	
8	9	10	11		38	39	40	41	
12	13	14	15		42	43	44	45	
16	17	18	19		46	47	48	49	
 					50	51	52	53	
 					54	55	56	57	
 					58	59	60	61	
 					62	63	64	65	
 					66	67	68	69

Note that splitting the problem into two separate functions actually made the code easier.

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.