6 Replies - 18145 Views - Last Post: 20 March 2011 - 01:10 AM Rate Topic: -----

#1 passer_by   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

How to print a list of words in the same line ,but separated

Posted 19 March 2011 - 08:26 AM

Hi guys ,

I'm new in Python , so go easy on me : )

I'd like to print on the screen the following list , however , not in a way that
every word has its own line .I need that all the words would be in the same line , with a "," between
every two words .

I've tried :

 class Base:

    def __init__(self):
        self.m_dataBase = [] # an empty list 
   
      
    def displayFingers(self):
        s = ''.join(self.m_dataBase )
        return s



but it's not working .

Let's take for example:

b=["one","two","three"]


then what needs to be printed is :
one, two, three


I'd appreciate your help
Regards ,Ron

Is This A Good Question/Topic? 1
  • +

Replies To: How to print a list of words in the same line ,but separated

#2 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to print a list of words in the same line ,but separated

Posted 19 March 2011 - 08:43 AM

I'm sure there are a few ways to do it, but how I would do it is:

b = ["one", "two", "three"]

for item in b:
 print item,

...

one two three



The comma after the print statement means the output will be on the same line.

If you want commas in your output, this would do it:

b = ["one", "two", "three"]

for item in b:
    if item == b[-1]: #The last element in b
        print item 
    else:
        print item + ",", #Add a comma

...

one, two, three



Is that what you meant? Hope it helps :)
Was This Post Helpful? 1
  • +
  • -

#3 passer_by   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: How to print a list of words in the same line ,but separated

Posted 19 March 2011 - 09:19 AM

View PostSimown, on 19 March 2011 - 08:43 AM, said:

I'm sure there are a few ways to do it, but how I would do it is:

b = ["one", "two", "three"]

for item in b:
 print item,

...

one two three



The comma after the print statement means the output will be on the same line.

If you want commas in your output, this would do it:

b = ["one", "two", "three"]

for item in b:
    if item == b[-1]: #The last element in b
        print item 
    else:
        print item + ",", #Add a comma

...

one, two, three



Is that what you meant? Hope it helps :)

First,thanks for the help.
But it's not working , with Python ver 3 at least .

b = ["one", "two", "three"]

for item in b:
 [s]print item,[/s] print (item),



The line
print item,
doesn't compile .I've tried with
 print (item),
but
the words are printed each one in a different line .Any other idea?

10x man
Was This Post Helpful? 0
  • +
  • -

#4 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to print a list of words in the same line ,but separated

Posted 19 March 2011 - 09:34 AM

I should have mentioned it was for Python 2.x, oops. In Python 3, print is now a function it's been made even easier!

#Python 3.2

b = ["one", "two", "three"]

>>> print(*b, sep = ", ", end = ".\n")

...

one, two, three.




Note the asterisk (*) before the list name. sep and end are the seperator and the ending value respectively.

This post has been edited by Simown: 19 March 2011 - 09:44 AM

Was This Post Helpful? 1
  • +
  • -

#5 passer_by   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: How to print a list of words in the same line ,but separated

Posted 19 March 2011 - 09:44 AM

Okay Simown I think it's okay,I've used
out+=item


works great.thanks a lot ,Ron

This post has been edited by passer_by: 19 March 2011 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,264
  • Joined: 23-December 08

Re: How to print a list of words in the same line ,but separated

Posted 19 March 2011 - 02:22 PM

my favorite technique, the accumulator approach!

def formatList(inputList):
    retString = ""
    for item in inputList:
        retString+= str(item)+", "
    return retString[:-2]

myList = [1,2,3,4,5]
print(formatList(myList))


This post has been edited by atraub: 19 March 2011 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 LiveToCode   User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 37
  • Joined: 17-March 10

Re: How to print a list of words in the same line ,but separated

Posted 20 March 2011 - 01:10 AM

", ".join(self.m_database)

That should get the job done.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1