473,322 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

printing contents of a list on one line

Hi,

Just wondering if someone can tell me how I could iterate through a list and
print each item next to one another.

for example :

If I have a list a = [1,2,3,4,5]

I could of course do this :

Expand|Select|Wrap|Line Numbers
  1. for item in list :
  2.          print item 
  3.  
but that prints each item on a new row .... what I want is to print each item so
it reads like this :

12345

with no new line after printing each item.

Thanks,
Apr 21 '08 #1
4 45333
ghostdog74
511 Expert 256MB
try this
print item ,
Apr 21 '08 #2
jlm699
314 100+
You should try not to use list as the variable name for your lists since it is a reserved word in Python.

Here's a tiny list comprehension that does the same thing as ghostdog's minus the spaces:
Expand|Select|Wrap|Line Numbers
  1. >>> ll = [1,2,3,4,5]
  2. >>> print ''.join([str(item) for item in ll])
  3. 12345
  4. >>> 
  5.  
w/o list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> ll = [1,2,3,4,5]
  2. >>> txt = ''
  3. >>> for item in ll:
  4. ...     txt += str(item)
  5. ...     
  6. >>> print txt
  7. 12345
  8. >>> 
Apr 21 '08 #3
You should try not to use list as the variable name for your lists since it is a reserved word in Python.

Here's a tiny list comprehension that does the same thing as ghostdog's minus the spaces:
Expand|Select|Wrap|Line Numbers
  1. >>> ll = [1,2,3,4,5]
  2. >>> print ''.join([str(item) for item in ll])
  3. 12345
  4. >>> 
  5.  
w/o list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> ll = [1,2,3,4,5]
  2. >>> txt = ''
  3. >>> for item in ll:
  4. ...     txt += str(item)
  5. ...     
  6. >>> print txt
  7. 12345
  8. >>> 
That worked perfectly ... thanks a million.
Apr 21 '08 #4
Smygis
126 100+
Expand|Select|Wrap|Line Numbers
  1. >>> li = range(10)
  2. >>> print reduce(lambda x, y: str(x) + str(y), li)
  3. 0123456789
  4.  
ftw!
Apr 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jeroen van vliet | last post by:
Hello, How do i print the contents of a textbox??? Jeroen
3
by: Alison | last post by:
I am new to OO programming and learning C#. I am trying to print out the contents of an object, but am only seeing the reference to the object. The accounts array contains Account objects, which...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
2
by: Michael Jasn | last post by:
Hi all, I am trying to print the contents of a std::list<Point> in gdb (actually ddd). I have this function void BezierCurveEvaluator::evaluateCurve(const std::vector<Point>& ptvCtrlPts) ...
4
by: Grahammer | last post by:
This is a stupid question, but the examples I've seen don't make sense to me... I simply want to print the contents of a textbox to a printer when the user clicks a button on my form. Printing...
5
by: Urs Vogel | last post by:
Hi When printing coloured text from a RichTextbox, it turns out to be a quite and timeconsuming job to position all differently coloured strings on the Page with DrawString(). Is there a...
2
by: | last post by:
Is there a way I can list the contents of all the EventArgs passed to a specific method? Thanks, Ben
5
by: HMS Surprise | last post by:
I want to print a count down timer on the same line. I tried print '\r', timeLeft, which just appends to the same line. thanx,
0
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2 for windows application. I have to add a new form to my application. This will will process several records from a datatable. In this form, 1. I need to have a log...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.