Is there a commas-in-between idiom?

  • Thread starter =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=
  • Start date
?

=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

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
 
J

James Stroud

Ernesto said:
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
 
?

=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

Ernesto said:
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(str, mylist))

Great solution!

Thank all of you,
Ernesto
 
T

Tim Peters

]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?

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
 
?

=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

Tim said:
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
 
G

Georg Brandl

Peter said:
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?

Do you never have a situation where you want to assign the same value
to two variables?

Or are you objecting to the fact that both names point to the same object?
It couldn't be otherwise. Consider:

X = []
A = B = X

What should this do? Copy "X" and assign one copy to A, one to B?

Georg
 
G

Gabriel Genellina

At said:
"""
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?

As an optimization, inside a method, you can bind an instance
attribute and a local name to the same object:

def some_action(self):
self.items = items = []
// following many references to self.items,
// but using items instead.

Names in the local namespace are resolved at compile time, so using
items is a lot faster than looking for "items" inside self's
namespace each time it's used.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
P

Peter van Kampen

Peter said:
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?

Do you never have a situation where you want to assign the same value
to two variables?

In the sense that I might want two empty lists or maybe 2 ints that
are initialised as 0 (zero). That's what I (incorrectly) thought A = B
= [] did.
Or are you objecting to the fact that both names point to the same
object?

I'm not objecting to anything, merely wondering when I could/should
use this idiom. There's a nice example in another response (self.items
= items = [])
It couldn't be otherwise. Consider:

X = []
A = B = X

What should this do? Copy "X" and assign one copy to A, one to B?

Ah yes, I see the error in my ways...I skipped the A = B part too
lightly.

Thanks,

PterK
 
P

Peter van Kampen

At said:
"""
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?

As an optimization, inside a method, you can bind an instance
attribute and a local name to the same object:

def some_action(self):
self.items = items = []
// following many references to self.items,
// but using items instead.

Names in the local namespace are resolved at compile time, so using
items is a lot faster than looking for "items" inside self's
namespace each time it's used.

Nice example.

Thanks,

PterK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top