Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove comma from tuples in python.

3,427 views
Skip to first unread message

Jaydeep Patil

unread,
Feb 21, 2014, 1:49:01 AM2/21/14
to
I am getting below tuple from excel.
How should i remove extra commas in each tuple to make it easy for operations.

tuples is:
seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))



please suggest me solution.



Regards
jay

Mircescu Andrei

unread,
Feb 21, 2014, 2:02:58 AM2/21/14
to
i think you have a tuple of tuples there. a tuple of 12 tuples.

you need to parse each one and represent it as you wish

Stephane Wirtel

unread,
Feb 21, 2014, 2:11:03 AM2/21/14
to Mircescu Andrei, pytho...@python.org
This is just a tuple of integers and not a tuple of tuples of integers, the parentheses around the number is just there for the evaluation.
> --
> https://mail.python.org/mailman/listinfo/python-list

Bernd Nawothnig

unread,
Feb 21, 2014, 2:20:31 AM2/21/14
to
On 2014-02-21, Mircescu Andrei wrote:
> vineri, 21 februarie 2014, 08:49:01 UTC+2, Jaydeep Patil a scris:
>> I am getting below tuple from excel.
>>
>> How should i remove extra commas in each tuple to make it easy for operations.
>>
>>
>>
>> tuples is:
>>
>> seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>
> i think you have a tuple of tuples there. a tuple of 12 tuples.

No it isn't:

#v+
>>> a = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>>> a
(0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11)
#v-

The comma makes a tuple, not the parenthesis alone:


#v+
>>> a = ((0.0,), (0.01,), (0.02,), (0.03,), (0.04,), (0.05,), (0.06,), (0.07,), (0.08,), (0.09,), (0.1,), (0.11,))
>>> a
((0.0,), (0.01,), (0.02,), (0.03,), (0.04,), (0.05,), (0.06,), (0.07,), (0.08,), (0.09,), (0.1,), (0.11,))
>>>
#v-




Bernd

--
no time toulouse

Gary Herron

unread,
Feb 21, 2014, 3:27:24 AM2/21/14
to pytho...@python.org
There are no extra *commas* there. Perhaps you mean extra
*parentheses*? When Python parses that line, the extra parentheses are
used to control the evaluation (unnecessarily in this case, as it turns
out), and won't be in the final result.

>>> seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05),
(0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
>>> seriesxlist1
(0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11)

A bit of notation, because I''m not sure we are communicating well here:
A tuple is a Python data structure. It has no commas or
parentheses. The *printing* of a Python tuple uses both for it's
appearance on the output, but the tuple itself has no such thing.

Gary Herron


Jussi Piitulainen

unread,
Feb 21, 2014, 4:13:30 AM2/21/14
to
Gary Herron writes:

> On 02/20/2014 10:49 PM, Jaydeep Patil wrote:
> > I am getting below tuple from excel.
> > How should i remove extra commas in each tuple to make it easy for
> > operations.
> >
> > tuples is:
> > seriesxlist1 = ((0.0), (0.01), (0.02), (0.03), (0.04), (0.05), (0.06), (0.07), (0.08), (0.09), (0.1), (0.11))
> >
> > please suggest me solution.
>
> There are no extra *commas* there. Perhaps you mean extra

There were extra commas in a previous thread.

Jaydeep, Rustom Mody gave you the answer, which you even quoted but
apparently failed to notice. Go back and see.

That answer was this:

>>> seriesxlist1 = ((0.0,), (0.01,), (0.02,))
>>> x2 = [x*x for (x,) in seriesxlist1]

I tend to omit those parentheses and use just the comma:

>>> x2 = [x*x for x, in seriesxlist1]

Alister

unread,
Feb 21, 2014, 4:29:08 AM2/21/14
to
I had not though of using unpacking in this way & would have written

x2= [x[0]**2 for x in serisexlist1]

I am not sure which is easier to read in this instance (single element
tupple) but unpacking would definitely be the way to go if the tupple had
multiple values.





--
Q: What do they call the alphabet in Arkansas?
A: The impossible dream.

Tim Chase

unread,
Feb 21, 2014, 9:00:32 AM2/21/14
to Alister, pytho...@python.org
On 2014-02-21 09:29, Alister wrote:
> > >>> seriesxlist1 = ((0.0,), (0.01,), (0.02,))
> > >>> x2 = [x*x for (x,) in seriesxlist1]
> >
> > I tend to omit those parentheses and use just the comma:
> >
> > >>> x2 = [x*x for x, in seriesxlist1]
>
> I had not though of using unpacking in this way & would have written
>
> x2= [x[0]**2 for x in serisexlist1]
>
> I am not sure which is easier to read in this instance (single
> element tupple) but unpacking would definitely be the way to go if
> the tupple had multiple values.

With the single-value tuple, I tend to find the parens make it more
readable, so I'd go with

[x*x for (x,) in lst]

whereas if they were multi-value tuples, I tend to omit the parens:

[x*y for x,y in lst]

though, tangentially, Python throws a SyntaxError if you try and pass
a generator to a function without extra outer parens because it
makes parsing them ambiguous otherwise:

>>> x = sum(a+b for a, b in lst, 10)
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole
argument
>>> x = sum((a+b) for a,b in lst), 10)
[no error]

-tkc


Mark Lawrence

unread,
Feb 21, 2014, 9:02:33 AM2/21/14
to pytho...@python.org
On 21/02/2014 08:27, Gary Herron wrote:
>
> A bit of notation, because I''m not sure we are communicating well here:
> A tuple is a Python data structure. It has no commas or
> parentheses. The *printing* of a Python tuple uses both for it's
> appearance on the output, but the tuple itself has no such thing.
>

>>> a = 1,2,3
>>> type(a)
<class 'tuple'>

I see commas and a tuple above but I don't see a print.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


Peter Otten

unread,
Feb 21, 2014, 9:14:39 AM2/21/14
to pytho...@python.org
Tim Chase wrote:

> With the single-value tuple, I tend to find the parens make it more
> readable, so I'd go with
>
> [x*x for (x,) in lst]

Hardly ever seen in the wild, but unpacking works with [...], too:

>>> items = zip(range(5))
>>> [x*x for [x] in items]
[0, 1, 4, 9, 16]


Roy Smith

unread,
Feb 21, 2014, 9:32:18 AM2/21/14
to
In article <mailman.7230.1392992...@python.org>,
Peter Otten <__pet...@web.de> wrote:


> [x*x for (x,) in lst]
>
> [paraphrasing...] can be better written as:
>
> [x*x for [x] in items]

I'm torn between, "Yes, the second form is distinctly easier to read"
and, "If you think the second form is easier to read, you're admitting
you're not really fluent in Python".

Travis Griggs

unread,
Feb 21, 2014, 12:48:42 PM2/21/14
to pytho...@python.org
I’ve used the comma form with struct.unpack() frequently:

count, = struct.unpack(‘!I’, self.packet)

That’s after I don’t use it and end up scratching my head for a while and finally remember that unpack returns a tuple regardless of how many things I unpack from it. It’s just natural if you’re doing lots of single unpacks to think it returns a single value. Either way, I much prefer it to:

count = struct.unpack(‘!I’, self.packet)[0]

Tim Chase

unread,
Feb 21, 2014, 1:07:00 PM2/21/14
to Travis Griggs, pytho...@python.org
On 2014-02-21 09:48, Travis Griggs wrote:
> I’ve used the comma form with struct.unpack() frequently:
>
> count, = struct.unpack(‘!I’, self.packet)

This is *especially* one of those places I want extra parens to make
sure I see what's happening. I've been stung too many times by the
easy-to-miss nature of just a single comma.

-tkc

Cameron Simpson

unread,
Feb 23, 2014, 8:19:02 PM2/23/14
to pytho...@python.org
+1 QOTW
--
Cameron Simpson <c...@zip.com.au>

It is a tale told by an idiot, full of sound and fury, signifying nothing.
- William Shakespeare
0 new messages