473,774 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing list

How do you print elements of the list in one line?

alist = [1, 2, 5, 10, 15]

so it will be like this:
1, 2, 5, 10, 15

because if I use this code

for i in alist:
print i

the result would be like this

1
2
5
10
15

Thanks.

May 7 '06 #1
5 2168
compboy wrote:
How do you print elements of the list in one line?

alist = [1, 2, 5, 10, 15]

so it will be like this:
1, 2, 5, 10, 15

because if I use this code

for i in alist:
print i

the result would be like this

1
2
5
10
15

Thanks.


Well, first, if you just print alist you'll get

[1, 2, 5, 10, 15]

which may be good enough. If that's not what you want then you can suppress the automatic RETURN that follows a print's output by adding a trailing comma to the print statement, like this

for i in alist:
print i,

1 2 5 10 15

Gary Herron

May 7 '06 #2
compboy wrote:
How do you print elements of the list in one line?

alist = [1, 2, 5, 10, 15]

so it will be like this:
1, 2, 5, 10, 15

print ', '.join(alist)

1, 2, 5, 10, 15

-tkc


May 7 '06 #3
compboy wrote:
How do you print elements of the list in one line?

alist = [1, 2, 5, 10, 15]

so it will be like this:
1, 2, 5, 10, 15

because if I use this code

for i in alist:
print i

the result would be like this

1
2
5
10
15

Thanks.


There are a number of ways to do it but if you want a one-liner:

print repr(alist)[1:-1]

will meet your spec.
May 7 '06 #4
Tim Chase wrote:
compboy wrote:
How do you print elements of the list in one line?

alist = [1, 2, 5, 10, 15]

so it will be like this:
1, 2, 5, 10, 15


>>> print ', '.join(alist) 1, 2, 5, 10, 15


???

Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
[GCC 3.3.4] on linux2
Type "help", "copyright" , "credits" or "license" for more
information.
a=[1,2,3,4,5]
print ', '.join (a) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found print ', '.join ('%d'%x for x in a)

1, 2, 3, 4, 5
May 7 '06 #5
On Sun, 07 May 2006 18:16:22 -0400,
Mel Wilson <mw********@sym patico.ca> wrote:
Tim Chase wrote:
compboy wrote:
How do you print elements of the list in one line?

alist = [1, 2, 5, 10, 15]

so it will be like this:
1, 2, 5, 10, 15
>>> print ', '.join(alist)

1, 2, 5, 10, 15 ??? Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
[GCC 3.3.4] on linux2
Type "help", "copyright" , "credits" or "license" for more information. a=[1,2,3,4,5]
print ', '.join (a) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found print ', '.join ('%d'%x for x in a)

1, 2, 3, 4, 5


Or one of:

print ', '.join(str(x) for x in a)

print ', '.join(map(str, a))

both of which work if the list contains non-integer elements.

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
May 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4858
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been selected. For example, if I were to have 5 pages with every second page printing, I would get the following results: Page 1 = Print OK Page 2 = Blank Page 3 = Print OK Page 4 = Blank
0
1498
by: JF Turcotte | last post by:
Hi I'm unsuccessfully trying to print a form's image under VB.NET. To print under .NET is a real pain in the , I find it to be complex, lenghty, confusing, upsetting and ultimately not to be working at all. My strategy is first to save the form's image as a bitmap (this works, I can save the image to a file). Then I use the printer's Graphics object's drawImage method to send it
1
2089
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 line is where it is falling over.
0
1296
by: ALMA_J_III | last post by:
Hi all! Customer will use HP LaserJet 4200TN printer for printing of invoices. 3 different versions of invoice on different color of paper should be to print. HP LaserJet 4200TN have 3 paper source trays. Maybe some from you have experience in printing to different trays of this printer. Crystalreport Report object has PrintOptions class and the next papersource properties:...
6
482
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having trouble getting it to print on multiple pages. The second page will not print at all. Thanks in advance
0
1694
by: Hank | last post by:
We have been printing pdf files through Adobe, from Access for several years. Adobe version 5.0 is currently installed. Recently we have received PDF files that were created under the Adobe 7.0 version. When trying to view a file manually Adobe 5.0 displays a pop up message: "This file may contain newer information than this viewer can support. It may not open or display correctly". There is a check box with the pop-up that says,...
3
1365
by: William Chang | last post by:
Is the different behavior between __repr__ and __str__ intentional when it comes to printing lists? Basically I want to print out a list with elements of my own class, but when I overwrite __str__, __str__ doesn't get called but if I overwrite __repr__, __repr__ will get called. Is this a bug? For example:
2
9976
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default printer settings to print. I wanted the print preview to appear the same for all users (i.e. a default page size of 8.5x14 (legal) and portrait mode). Many users have different printers as their default (plotters etc..) and I found that various...
4
16824
ADezii
by: ADezii | last post by:
Recently, there seems to be several questions specifically related to Printers and changing Printing characteristics for Forms and Reports. For this reason alone, I decided to dedicate this week's Tip to these Topics. The Tip will actually consist of several Tips which I feel are very useful for all Users, from Newbies to Experts. In order to utilize the code contained within these Tips, you must have Access 2002 or later. How can I...
1
13766
by: Glenn | last post by:
I am writing a program for field work that will use a receipt printer. I need to be able to adjust the page settings prior to printing depending on how much needs to be printed. I have been able to print to this printer but cannot figure out how to overwrite the page settings. Below is the code so far: Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
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
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.