473,549 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing contents of a list on one line

32 New Member
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 45341
ghostdog74
511 Recognized Expert Contributor
try this
print item ,
Apr 21 '08 #2
jlm699
314 Contributor
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
fordie1000
32 New Member
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 New Member
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
4517
by: Jeroen van vliet | last post by:
Hello, How do i print the contents of a textbox??? Jeroen
3
4060
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 consist of an Id, Owner and Balance. My code is printing out Account Name: ass1.Account instead of Account Name: John Smith
1
2070
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 variable or With block variable not set. I am trying to print the contents of a list box on another form. This is the sub, and the highlighted...
2
7065
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) When I break into the function and say,
4
4028
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 to the default printer is fine, but eventually I will want the user to be able to choose a printer (not necessarily at print time). Can someone...
5
2369
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 better method of achieving this, or are there samples somewhere? Couldn't find anything on MSDN.
2
1360
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
5089
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
978
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 file of each record's process result, PRINTED LINE BY LINE, LIVE, ON THE SCREE OF THE FORM, as the process of each record continues. 2. I need...
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.