de-diac
de-diac

Reputation: 439

Print list items with a header

If I have a list defined as list=['Ford','Mustang','1966','red'] and try to print it my output would look like: Ford Mustang 1966 red

But how can I achieve to have a heading so the output would look like:

Brand Modell Year Color
Ford Mustang 1966 Red

That would look much more professional.

Upvotes: 2

Views: 10353

Answers (4)

Hugh Bothwell
Hugh Bothwell

Reputation: 56684

LAYOUT = "{!s:10} {!s:14} {!s:4} {!s:10}"

yourList = ["Ford","Mustang",1966,"Red"]

print LAYOUT.format("Brand","Modell","Year","Color")
print LAYOUT.format(*yourList)

produces output in columns which will line up (so long as you don't exceed the field width specifiers!), ie

Brand      Modell         Year Color     
Ford       Mustang        1966 Red       
Mercedes   F-Cell         2011 White

Upvotes: 6

Meng
Meng

Reputation: 1

header = ['Brand', 'Modell', 'Year', 'Color']

car = ['Ford','Mustang','1966','red']

print("\t".join(header) + "\n" + "\t".join(car))

Upvotes: 0

KevinD
KevinD

Reputation: 23

I'm not a python expert, but couldn't you just create an associative list, where the keys are the header names?

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816930

You mean:

print 'Brand', 'Modell', 'Year', 'Color'
print ' '.join(yourList)

?

P.S.: Don't name your variable list!

Upvotes: 1

Related Questions