mohammad shahbaz Khan
mohammad shahbaz Khan

Reputation: 113

how to get result from many list in same line in python

I have three list:

alist=[1,2,3,4,5]
blist=['a','b','c','d','e']
clist=['@','#','$','&','*']

I want my output in this format:

1 2 3 4 5
a b c d e
@ # $ & *

I am able to print in correct format but when i am having list with many elements it's actually printing like this:

1 2 3 4 5 6 ..........................................................................
................................................................................

a b c d e ............................................................................
......................................................................................

@ # $ & * .............................................................................
.......................................................................................

but I want my output like this:

12345....................................................................
abcde...................................................................
@#$&*...................................................................

............................................................... {this line is from alist}
................................................................ {this line is from blist}
................................................................ {this line is from clist}

Upvotes: 2

Views: 62

Answers (1)

Lav
Lav

Reputation: 2274

Try the following:

term_width = 80
all_lists = (alist, blist, clist)
length = max(map(len, all_lists))
for offset in xrange(0, length, term_width):
    print '\n'.join(''.join(map(str, l[offset:offset+term_width])) for l in all_lists)

This assumes terminal width is 80 characters, which is the default. You might want to detect it's actual width with curses library or something based on it.

Either way, to adapt to any output width you only need to change term_width value and the code will use it.

It also assumes all elements are 1-character long. If it's not the case, please clarify.

If you need to detect terminal width, you may find some solutions here: How to get Linux console window width in Python

Upvotes: 2

Related Questions