10 Replies - 2741 Views - Last Post: 21 September 2012 - 09:15 PM Rate Topic: -----

#1 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Printing elements in a list on one line using for loop w/ formatting

Posted 20 September 2012 - 05:55 PM

Hello all! So I'm having some trouble here. I made an empty list, then a user data which got appended into the list. Now, I'm trying to print the elements in the list with formatting (like %8s %6s etc.) on one line, but I don't know how to do it without making the code longer than I have to.

Here's an example of what I have now:

 
 car_Sales = []

 #for loop goes here with user inputted stuff appended into the list.
 
 print "Car sales by quarter: %8s %6s %6s %6s" %(car_Sales[0], car_Sales[1], car_Sales[2], car_Sales[3])



I would like to condense it in a for loop, having something like print "Car sales by quarter: %8s", %car_Sales[i]
all on one line. So it would be displayed like:

Car sales by quarter: 25 53 78 98

The other problem I have is the formatting. I'm trying to get the 1st element printed to be justified 8 spaces over, while the rest just have 6.

Any help would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Printing elements in a list on one line using for loop w/ formatting

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 20 September 2012 - 09:43 PM

I had an idea for this one that was really fun!

>>> cars = ['1000', '1001', '1002', '1003', '1004']#basic list

>>> outString = "Car sales by quarter: %8s" +" %6s"*(len(cars)-1)#first element is set to 8 spaces, then 6 spaces for each one after that

>>> print(outString) #confirm that we have 5 string placeholders
Car sales by quarter: %8s %6s %6s %6s %6s

>>> print(outString % tuple(cars)) #turn the list to a tuple ( for pythony reasons) and then use it!
Car sales by quarter:     1000   1001   1002   1003   1004

#printing it as a one-liner
>>> print(("Car sales by quarter: %8s" +" %6s"*(len(cars)-1))%tuple(cars))
Car sales by quarter:     1000   1001   1002   1003   1004



Winning :)

EDIT:
I really did get a kick out of this one.

This post has been edited by atraub: 20 September 2012 - 10:07 PM

Was This Post Helpful? 1
  • +
  • -

#3 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 09:12 AM

Ah! That does work, but unfortunately I can't use tuples :( Is there any other way?

Thank you for your response though! I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 09:30 AM

My friend, you used a tuple first! What do you think (car_Sales[0], car_Sales[1], car_Sales[2], car_Sales[3]) is? Values separated by commas in this fashion are tuples!

I understand, you can't use the tuple function, I'll have to think about this.

This post has been edited by atraub: 21 September 2012 - 09:31 AM

Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5943
  • View blog
  • Posts: 12,871
  • Joined: 16-October 07

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 09:42 AM

Simply:
print("Car sales by quarter:  " + ''.join('{0:<6}'.format(s) for s in car_Sales))



Just pad a couple extra space on the title for 8, 6, 6...

Unless I'm missiing something.
Was This Post Helpful? 1
  • +
  • -

#6 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 10:22 AM

Pretty much can't use all of .join('{0:<6}'.format(s) for s in car_Sales)) either unfortunately. I was thinking doing something like ("Car sales by quarter: (insert 8 spaces here)") then put a coma after to continue printing on same line. So the next print statement will be ("%s %s %s %s) car_Sales[i] in a for loop. However when I tried it, it only printed 2 or 3 of the values. Then said something like it exceeds the length of the list.
Was This Post Helpful? 0
  • +
  • -

#7 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 10:32 AM

Err I meant the next print statement would be (%s), %car_Sales[i] in a for loop.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5943
  • View blog
  • Posts: 12,871
  • Joined: 16-October 07

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 10:45 AM

So, you, um, can't use Python?!? I am confused.

Right, can you at least use a regular for loop?
s = "Car sales by quarter:  "
for item in car_Sales:
	s += '{0:<6}'.format(item)
print(s)



You probably want to use the older, deprecated syntax:
s = "Car sales by quarter:  "
for item in car_Sales:
	s += '%6s' % item
print(s)



Note that python prefers a join on a string list to a string concat.
Was This Post Helpful? 1
  • +
  • -

#9 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 11:18 AM

Yes! Sorry, I have to use the older syntax for right now. I should have mentioned that earlier.
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 11:35 AM

View Postblindchicken11, on 21 September 2012 - 02:18 PM, said:

Yes! Sorry, I have to use the older syntax for right now. I should have mentioned that earlier.
Why?

This post has been edited by atraub: 21 September 2012 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Printing elements in a list on one line using for loop w/ formatting

Posted 21 September 2012 - 09:15 PM

Baavgai that is exactly what I needed. Thank you so much and thank you atraub as well! It was making me so mad that I couldn't remember how to do that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1