sam
sam

Reputation: 7

Arranging data in tabular format from list

I have a list as below and looking to display it in 5 columns one under the other

lsit = ['Alexander City', 'Andalusia', 'Anniston', 'Athens', 'Atmore', 
        'Auburn', 'Bessemer', 'Birmingham', 'Chickasaw', 'Clanton', 
        'Cullman', 'Decatur', 'Demopolis', 'Dothan', 'Enterprise', 
        'Eufaula', 'Florence', 'Fort Payne', 'Gadsden', 'Greenville',
        'Guntersville', 'Huntsville']

Tried with print("\t".join(lsit)) but the data is not populated properly

>>> print("\t".join(lsit))
Alexander City  Andalusia   Anniston    Athens  Atmore  Auburn  Bessemer    Birmingham  Chickasaw   Clanton Cullman DecaturDemopolis    Dothan  Enterprise  Eufaula Florence    Fort Payne  Gadsden Greenville  Guntersville    Huntsville

Can someone guide how can this be achieved in python?

Upvotes: 1

Views: 40

Answers (2)

Md.Rezaul Karim
Md.Rezaul Karim

Reputation: 46

You can used this solution:

lsit = [['Alexander City', 'Andalusia', 'Anniston', 'Athens', 'Atmore'], 
        ['Auburn', 'Bessemer', 'Birmingham', 'Chickasaw', 'Clanton'],
        ['Cullman', 'Decatur', 'Demopolis', 'Dothan', 'Enterprise'], 
        ['Eufaula', 'Florence', 'Fort Payne', 'Gadsden', 'Greenville'],
        ['Guntersville', 'Huntsville']]
for i in range(len(lsit)):
    for j in range(len(lsit[i])):
        print("\t",lsit[i][j], end=' ')
    print()

Upvotes: 0

Tomerikoo
Tomerikoo

Reputation: 19414

Well all you're missing is to print them in groups of five:

for i in range(0, len(lsit), 5):
    print("\t".join(lsit[i: i+5]))

Which gives:

Alexander City  Andalusia   Anniston    Athens  Atmore
Auburn  Bessemer    Birmingham  Chickasaw   Clanton
Cullman Decatur Demopolis   Dothan  Enterprise
Eufaula Florence    Fort Payne  Gadsden Greenville
Guntersville    Huntsville

Upvotes: 1

Related Questions