473,803 Members | 2,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a commas-in-between idiom?

Hi experts,

it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?

<code>

list = [1,2,3,4,5,6]

# the easy way
for element in list:
print element, ',',

print
# this is what I really want. is there some way better?
if (len(list) 0):
print list[0],
for element in list[1:]:
print ',', element,

</code>

Thx,
Ernesto
Nov 5 '06 #1
14 1454
Ernesto García García <ti************ **********@gmai l.comwrites:
Hi experts,

it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?
>>list = [1,2,3,4,5,6]
print ','.join(map(st r, list))
1,2,3,4,5,6

--
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk | Visit us at: http://www.gmta.info
Nov 5 '06 #2
Ernesto García García wrote:
it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?
>>items = [1, 2, 3, "many"]
print ", ".join(str(item ) for item in items)
1, 2, 3, many

Peter
Nov 5 '06 #3
Ernesto García García wrote:
Hi experts,

it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?

<code>

list = [1,2,3,4,5,6]

# the easy way
for element in list:
print element, ',',

print
# this is what I really want. is there some way better?
if (len(list) 0):
print list[0],
for element in list[1:]:
print ',', element,

</code>

Thx,
Ernesto
print ",".joint(some_ list)

Where what you have named "list" is now called "some_list" because it is
terribly ill-advised to reassign the name of a built-in type.

James
Nov 5 '06 #4
Ernesto García García wrote:
Hi experts,

it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?

<code>

list = [1,2,3,4,5,6]

# the easy way
for element in list:
print element, ',',

print
# this is what I really want. is there some way better?
if (len(list) 0):
print list[0],
for element in list[1:]:
print ',', element,

</code>

Thx,
Ernesto
mylist = [1,2,3,4,5,6]
print ','.join(map(st r, mylist))

Great solution!

Thank all of you,
Ernesto
Nov 5 '06 #5
]Ernesto García García]
it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?

<code>

list = [1,2,3,4,5,6]

# the easy way
for element in list:
print element, ',',

print
# this is what I really want. is there some way better?
if (len(list) 0):
More idiomatic as

if len(list) 0:

and even more so as plain

if list:
print list[0],
for element in list[1:]:
print ',', element,
Do you really want a space before and after each inter-element comma?
</code>
An often-overlooked alternative to playing with ",".join() is:

print str(list)[1:-1]

That is, ask Python to change the list into a big string, and just
strip the brackets off each end:
>>alist = ['a, bc', 4, True]
print str(alist)[1:-1]
'a, bc', 4, True

Note the quotes around the string element! This differs from what
your code snippet above would produce (in addition to differing wrt
spaces around inter-element commas):

a, bc , 4 , True
Nov 5 '06 #6
Tim Peters wrote:
More idiomatic as

if len(list) 0:

and even more so as plain

if list:
> print list[0],
for element in list[1:]:
print ',', element,


Do you really want a space before and after each inter-element comma?
No, but it was only an example. I usually go for string concatenation.
An often-overlooked alternative to playing with ",".join() is:

print str(list)[1:-1]
That's funny! Not that I like it more that the join solution, but funny
nevertheless.

Thank you,
Ernesto
Nov 5 '06 #7
Ernesto García García wrote:
it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?
I've collected a bunch of list pydioms and other notes here:

http://effbot.org/zone/python-list.htm

For formatting issues, see:

http://effbot.org/zone/python-list.htm#printing

</F>

Nov 6 '06 #8
I've collected a bunch of list pydioms and other notes here:
>
http://effbot.org/zone/python-list.htm
Thank you for the suggestion.

Ernesto
Nov 6 '06 #9
On 2006-11-06, Fredrik Lundh <fr*****@python ware.comwrote:
I've collected a bunch of list pydioms and other notes here:

http://effbot.org/zone/python-list.htm
"""
A = B = [] # both names will point to the same list
"""

I've been bitten by this once or twice in the past, but I have always
wondered what it was useful for? Can anybody enlighten me?

TIA,

PterK

--
Peter van Kampen
pterk -- at -- datatailors.com
Nov 8 '06 #10

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

Similar topics

6
1702
by: Kyle E | last post by:
I wrote a program that asks a user questions and records the answers and prints them out at the end. Pretty simple... but I have a few things that I don't like about it. ---------------------------------------------- print "Do you have a P.O. Box?" poan = raw_input ("> ") if poan == "yes": print "What is your P.O. Box number?" pobox = input ("> ") ----------------------------------------------
9
3756
by: Marek Mänd | last post by:
<style type="text/css"> q:after{content:',"'} </style> <q>This will be the shame of CSS</q> claimed Marek Mänd and added that <q>consumers expect to create generated content via CSS where there would be no comma right after HERE</q>
3
2994
by: Grey Plastic | last post by:
How do I pass text that contains a comma into a macro? For example, I have #define ATTRIBUTE(name,type) ... and I want to do ATTRIBUTE(transitions, map<int,int>); However, the last comma is being interpretted as the macro argument
3
1752
by: darren | last post by:
Hello, I am posting a html form to a php processing page, which produces a csv file from the form data. However if the user inputs a comma then it screws up my data. So i'd like to know how to strip my data of commas before submitting to the php processor. Or maybe not strip the commas but handle them better.
3
3434
by: Jon Maz | last post by:
Hi, A quick one: If you are forming a dynamic sql statement using parameters from a web form, you would normally double up any single inverted commas inputted by the user to stop sql injection. But if you are using command parameters to build the sql statement (as below), is this still necessary? cmd.Parameters.Add("@subcategory", SqlDbType.VarChar).Value =
9
8889
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces and commas, and can use quotes to encapsulate multiple words. An example: tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6" So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4...
3
2269
by: bowtie | last post by:
if a code is written like below what does it mean: project1.openform form1 ,,,, i'm mainly concerned about the commas wat do they imply and what can be put after these commas
4
1927
by: riccrom123 | last post by:
Hi, I have a csv file where the commas are skipped by the characther \ for example field1,field2,field3\,field3,field4 I would like to extract the fields and obtain field1 field2 field3field3 field4
1
1756
by: vdesio | last post by:
I am new to ASP. I am using the free ezscheduler asp program to create a calendar for my website. The idea is for employees to schedule their work shifts online. I have modified some of the programmin from its original format. Here is my problem. The information that people enter are going into a database as comma delimited. When the information is pulled from the database and placed on the website it leaves the commas from the fields. I do...
5
4780
by: Robert Dodier | last post by:
Hello, I'd like to split a string by commas, but only at the "top level" so to speak. An element can be a comma-less substring, or a quoted string, or a substring which looks like a function call. If some element contains commas, I don't want to split it. Examples: 'foo, bar, baz' ='foo' 'bar' 'baz'
0
10542
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10309
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
10289
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
10068
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...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
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
5496
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...
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
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.