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 : -
for item in list :
-
print item
-
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,
4 45341
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: - >>> ll = [1,2,3,4,5]
-
>>> print ''.join([str(item) for item in ll])
-
12345
-
>>>
-
w/o list comprehension: -
>>> ll = [1,2,3,4,5]
-
>>> txt = ''
-
>>> for item in ll:
-
... txt += str(item)
-
...
-
>>> print txt
-
12345
-
>>>
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: - >>> ll = [1,2,3,4,5]
-
>>> print ''.join([str(item) for item in ll])
-
12345
-
>>>
-
w/o list comprehension: -
>>> ll = [1,2,3,4,5]
-
>>> txt = ''
-
>>> for item in ll:
-
... txt += str(item)
-
...
-
>>> print txt
-
12345
-
>>>
That worked perfectly ... thanks a million.
-
>>> li = range(10)
-
>>> print reduce(lambda x, y: str(x) + str(y), li)
-
0123456789
-
ftw!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jeroen van vliet |
last post by:
Hello,
How do i print the contents of a textbox???
Jeroen
|
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
|
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...
|
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,
|
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...
| |
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.
|
by: |
last post by:
Is there a way I can list the contents of all the EventArgs passed to a
specific method?
Thanks,
Ben
|
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,
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |