Iakovl
Iakovl

Reputation: 1083

how to make a formated print of list with list within it in python

I have a class in python with 2 "main" strings and a list of strings in it with N size

How can I "nicely" format the print of it so i can print the pain list and it will look normal.

The expected result is like:

entry 1 : string1 string 2 <br>
liststring1 liststring2 .... liststring n

entry 2 : string1 string 2 <br>
liststring1 liststring2 .... liststring n

.<br>
.<br>
.<br>
entry n : string1 string 2 <br>
liststring1 liststring2 .... liststring n

Upvotes: 0

Views: 99

Answers (3)

Evan Hammer
Evan Hammer

Reputation: 460

Let's presume your python class, Entry, has the following member variables:

Entry.string1  
Entry.string2  
Entry.list_of_strings  

Then to print a list_of_entries:

>>> for entry in list_of_entries:
...     print entry, " : ", entry.string1, entry.string2
...     for s in entry:
...         print s,
...     print ""

Upvotes: 1

engineerC
engineerC

Reputation: 2868

Try the pprint module for easy printing of 2D lists, among other things.

import pprint
x=[[1,2,3,4,5,6,7,8,9,10,11],['jkdfjkdfjkfdjkfdkjfdkdfjfkdjdkfjkdjkfd']]
pprint.pprint(x)

Upvotes: 0

ev-br
ev-br

Reputation: 26040

>>> l
[[1, 2, 3], [1, 2, 3]]
>>> for entry in l:
...    print entry, ":", 
...    for x in entry:
...        print x,
...    print

Notice the trailing commas in print statements.

Upvotes: 0

Related Questions