Mailing List Archive

Puzzling difference between lists and tuples
I am puzzled by the reason for this difference between lists and tuples.

A list of with multiple strings can be reduced to a list with one string with the expected results:

for n in ['first','second']:
print n

for n in ['first']:
print n

The first loop prints "first", "second", and the second prints "first".

====

This is not true for a tuple:

for n in ('first','second'):
print n

for n in ('first'):
print n


prints "first", "second" in the first case, but "f","i","r","s","t" in the second.

Where is this inconsistency explained?

--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 2020-09-17 at 09:24:57 -0600,
William Pearson <william.pearson@gmail.com> wrote:

> for n in ('first'):

That's not a tuple. That's a string.

Try it this way:

for n in ('first',): # note the trailing comma
print n

Dan
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 9/17/20 8:24 AM, William Pearson wrote:

> I am puzzled by the reason for this difference between lists and tuples.
>
> A list of with multiple strings can be reduced to a list with one string with the expected results:

> for n in ['first']:
> print n

['first'] is a list.

> for n in ('first'):
> print n

('first') is not a tuple. The tuple operator is actually the comma:

>>> not_a_tuple = ('first')
>>> type(not_a_tuple)
<class 'str'>

>>> is_a_tuple = 'first',
>>> type(is_a_tuple)
<class 'tuple'>

I tend to use both as it makes it stand out a bit more:

>>> still_a_tuple = ('first', )
>>> type(still_a_tuple)
<class 'tuple'>

The only time the parentheses are required for tuple building is when
they would otherwise not be interpreted that way:

some_func('first', 'second') # some_func called with two str args

some_func(('first', 'second')) # some_func called with one tuple arg

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 9/17/20 11:24 AM, William Pearson wrote:
> I am puzzled by the reason for this difference between lists and tuples.
>
> A list of with multiple strings can be reduced to a list with one string with the expected results:
>
> for n in ['first','second']:
> print n
>
> for n in ['first']:
> print n
>
> The first loop prints "first", "second", and the second prints "first".
>
> ====
>
> This is not true for a tuple:
>
> for n in ('first','second'):
> print n
>
> for n in ('first'):
> print n
>
>
> prints "first", "second" in the first case, but "f","i","r","s","t" in the second.
>
> Where is this inconsistency explained?
>
Parenthesis don't always make a tuple (and in fact aren't needed to make
them)

('first') is just the string value 'first', inside a set of parenthesis
to possible affect order of operations.

For 1 element tuples, you need a trailing comma, like ('first,) and in
many places it also could be just 'first',

just like your first example could be just for n in 'first', 'second':

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 2020-09-17 17:47, Ethan Furman wrote:
> On 9/17/20 8:24 AM, William Pearson wrote:
>
>> I am puzzled by the reason for this difference between lists and tuples.
>>
>> A list of with multiple strings can be reduced to a list with one string with the expected results:
>
>> for n in ['first']:
>> print n
>
> ['first'] is a list.
>
>> for n in ('first'):
>> print n
>
> ('first') is not a tuple. The tuple operator is actually the comma:
>
> >>> not_a_tuple = ('first')
> >>> type(not_a_tuple)
> <class 'str'>
>
> >>> is_a_tuple = 'first',
> >>> type(is_a_tuple)
> <class 'tuple'>
>
> I tend to use both as it makes it stand out a bit more:
>
> >>> still_a_tuple = ('first', )
> >>> type(still_a_tuple)
> <class 'tuple'>
>
> The only time the parentheses are required for tuple building is when
> they would otherwise not be interpreted that way:
>
They're needed for the empty tuple, which doesn't have a comma.

> some_func('first', 'second') # some_func called with two str args
>
> some_func(('first', 'second')) # some_func called with one tuple arg
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 9/17/20 10:43 AM, MRAB wrote:
> On 2020-09-17 17:47, Ethan Furman wrote:

>> The only time the parentheses are required for tuple building is when
>> they would otherwise not be interpreted that way:
>>
> They're needed for the empty tuple, which doesn't have a comma.

Ah, right. Thanks.

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 2020-09-17, MRAB <python@mrabarnett.plus.com> wrote:
>> The only time the parentheses are required for tuple building is when
>> they would otherwise not be interpreted that way:
>>
> They're needed for the empty tuple, which doesn't have a comma.
>
>> some_func('first', 'second') # some_func called with two str args
>>
>> some_func(('first', 'second')) # some_func called with one tuple arg

Yea, the syntax for tuple literals has always been a bit of an ugly
spot in Python. If ASCII had only had one more set of open/close
brackets...

--
Grant



--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On Fri, Sep 18, 2020 at 10:53 AM Grant Edwards
<grant.b.edwards@gmail.com> wrote:
>
> On 2020-09-17, MRAB <python@mrabarnett.plus.com> wrote:
> >> The only time the parentheses are required for tuple building is when
> >> they would otherwise not be interpreted that way:
> >>
> > They're needed for the empty tuple, which doesn't have a comma.
> >
> >> some_func('first', 'second') # some_func called with two str args
> >>
> >> some_func(('first', 'second')) # some_func called with one tuple arg
>
> Yea, the syntax for tuple literals has always been a bit of an ugly
> spot in Python. If ASCII had only had one more set of open/close
> brackets...

... then I'd prefer them to be used for sets, actually. I think the
set/dict collision is more weird than the tuple/grouping one.

Now, if only ASCII had *two* more sets of open/close brackets...

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 2020-09-18, Chris Angelico <rosuav@gmail.com> wrote:
> On Fri, Sep 18, 2020 at 10:53 AM Grant Edwards
><grant.b.edwards@gmail.com> wrote:
>>
>> On 2020-09-17, MRAB <python@mrabarnett.plus.com> wrote:
>> >> The only time the parentheses are required for tuple building is when
>> >> they would otherwise not be interpreted that way:
>> >>
>> > They're needed for the empty tuple, which doesn't have a comma.
>> >
>> >> some_func('first', 'second') # some_func called with two str args
>> >>
>> >> some_func(('first', 'second')) # some_func called with one tuple arg
>>
>> Yea, the syntax for tuple literals has always been a bit of an ugly
>> spot in Python. If ASCII had only had one more set of open/close
>> brackets...
>
> ...then I'd prefer them to be used for sets, actually. I think the
> set/dict collision is more weird than the tuple/grouping one.
>
> Now, if only ASCII had *two* more sets of open/close brackets...

Rigth. I had forgotten about sets -- they're still a "new" feature in
my mind, and I rarely seem to run into situations where I use them.

There must be a few more sets of brackets in Unicode...

--
Grant


--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 19/09/20 6:49 am, Grant Edwards wrote:

> There must be a few more sets of brackets in Unicode...

Quite a lot, actually. The character browser in MacOSX is currently
showing me 17, not including the ones that are built up from
multiple characters.

Some of them could be easily confused with normal brackets, but
there are a few interesting ones, such as LEFT/RIGHT WHITE/BLACK
LENTICULAR BRACKET and LEFT/RIGHT TORTOISE SHELL BRACKET.

? ?? ?? ?
--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
William Pearson <william.pearson@gmail.com> writes:

> ...
> for n in ('first'):
> print n
>
>
> ... but "f","i","r","s","t" in the second.

#+BEGIN_SRC: python
for n in ('first',):
print n
#+BEGIN_SRC

Then, that will print 'first'. And please use Python3...

Sincerely, Byung-Hee

--
^????? _????_ ?????_^))//
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
18.09.20 03:55, Chris Angelico ????:
> On Fri, Sep 18, 2020 at 10:53 AM Grant Edwards
> <grant.b.edwards@gmail.com> wrote:
>> Yea, the syntax for tuple literals has always been a bit of an ugly
>> spot in Python. If ASCII had only had one more set of open/close
>> brackets...
>
> ... then I'd prefer them to be used for sets, actually. I think the
> set/dict collision is more weird than the tuple/grouping one.
>
> Now, if only ASCII had *two* more sets of open/close brackets...

Then I would use them for sets and frozensets.

--
https://mail.python.org/mailman/listinfo/python-list
RE: Puzzling difference between lists and tuples [ In reply to ]
There is a simple and obvious way to make sure you have a tuple by invoking the keyword/function in making it:

>>> a=('first')
>>> type(a)
<class 'str'>

>>> a=("first",)
>>> type(a)
<class 'tuple'>

>>> a=tuple("first")
>>> type(a)
<class 'tuple'>

That seems more explicit than adding a trailing comma. It also is a simple way to make an empty tuple but is there any penalty for using the function tuple()?


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net@python.org> On Behalf Of "???"
Sent: Saturday, September 19, 2020 11:39 PM
To: python-list@python.org
Subject: Re: Puzzling difference between lists and tuples

William Pearson <william.pearson@gmail.com> writes:

> ...
> for n in ('first'):
> print n
>
>
> ... but "f","i","r","s","t" in the second.

#+BEGIN_SRC: python
for n in ('first',):
print n
#+BEGIN_SRC

Then, that will print 'first'. And please use Python3...

Sincerely, Byung-Hee

--
^????? _????_ ?????_^))//
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 2020-09-20 23:59, Avi Gross via Python-list wrote:
> There is a simple and obvious way to make sure you have a tuple by invoking the keyword/function in making it:
>
>>>> a=('first')
>>>> type(a)
> <class 'str'>
>
>>>> a=("first",)
>>>> type(a)
> <class 'tuple'>
>
>>>> a=tuple("first")
>>>> type(a)
> <class 'tuple'>
>
> That seems more explicit than adding a trailing comma. It also is a simple way to make an empty tuple but is there any penalty for using the function tuple()?
>
[snip]
>>> tuple("first")
('f', 'i', 'r', 's', 't')

Not the same as ("first",).

A simpler way to make an empty tuple is just ().
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 21/09/20 10:59 am, Avi Gross wrote:
>>>> a=tuple("first")
>>>> type(a)
> <class 'tuple'>
>
> That seems more explicit than adding a trailing comma.

It doesn't do what you want, though:

>>> a = tuple("first")
>>> print(a)
('f', 'i', 'r', 's', 't')

If you really want to use tuple() to create a 1-tuple without
using a trailing comma, you can do it this way:

>>> a = tuple(["first"])
>>> print(a)
('first',)

But this costs you both a list creation and a tuple() call.
On my machine it seems to be about 17 times slower than using
a trailing comma:

>>> timeit.repeat("tuple(['first'])")
[0.17746889099998953, 0.17687880599999062, 0.17687711000002082,
0.1767632840000033, 0.17684489200001963]
>>> timeit.repeat("('first',)")
[0.01173928899999055, 0.011569334000000708, 0.011588000000017473,
0.011569761000032486, 0.011579383999958281]

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
RE: Puzzling difference between lists and tuples [ In reply to ]
('M','R','A','B') is correct. I appreciate the correction. I did not look to
see the content of what I created, just the type!

>>> a = tuple("first")
>>> a
('f', 'i', 'r', 's', 't')
>>> type(a)
<class 'tuple'>

But I thought adding a comma would help and it does not!

>>> b = tuple("first",)
>>> b
('f', 'i', 'r', 's', 't')

Yet something this simple without invoking tuple(), works!

>>> c = 'first',
>>> c
('first',)

So I read the manual page and tuple takes an iterable as an argument and
treats a string as an iterator on characters! It is not a simple
initializer.

I got around it, sort of, using n array with a single object of type string
in it so the iterator is iterating at a different level.

>>> d = ["first"]
>>> tuple(d)
('first',)
>>> tuple(["first"])
('first',)

I understand the design choice and can imagine there may be another function
that initializes a tuple more directly in some module.

Returning to lurking mode ...

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net@python.org> On
Behalf Of MRAB
Sent: Sunday, September 20, 2020 7:35 PM
To: python-list@python.org
Subject: Re: Puzzling difference between lists and tuples

On 2020-09-20 23:59, Avi Gross via Python-list wrote:
> There is a simple and obvious way to make sure you have a tuple by
invoking the keyword/function in making it:
>
>>>> a=('first')
>>>> type(a)
> <class 'str'>
>
>>>> a=("first",)
>>>> type(a)
> <class 'tuple'>
>
>>>> a=tuple("first")
>>>> type(a)
> <class 'tuple'>
>
> That seems more explicit than adding a trailing comma. It also is a simple
way to make an empty tuple but is there any penalty for using the function
tuple()?
>
[snip]
>>> tuple("first")
('f', 'i', 'r', 's', 't')

Not the same as ("first",).

A simpler way to make an empty tuple is just ().
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On 20Sep2020 20:33, Avi Gross <avigross@verizon.net> wrote:
>('M','R','A','B') is correct. I appreciate the correction. I did not look to
>see the content of what I created, just the type!
>
>>>> a = tuple("first")
>>>> a
>('f', 'i', 'r', 's', 't')
>>>> type(a)
><class 'tuple'>
>
>But I thought adding a comma would help and it does not!

Ah, the comma is for an expression. But you made a function call (well,
"tuple" is a type and types are callable to make instances).

In a function call the parmeter separating commas have higher precedence
than the commas with defines a tuple, you you've made a function call
with one argument "first", not a function call with one _tuple_ argument
("first",).

Consider:

foo(1, 2)

That supplies 2 arguments, not a single tuple.

As with other situations where the default precedence groups things in a
different way from your intent, brackets get required here if you want
to express a tuple.

This:

foo( (1,2) )

isn't notionally different from needing brakcets to express this:

(3+5) * 8

which means something else without its brackets.

Also, function calls allow you to include a trailing comma because it
helps with code. Consider the following bigger call:

x = foo(
1,
2,
fred=5,
)

laid out on separate lines for readability (a little redundant here, but
some complex function calls, or those exceeding the deired line length,
are often folded this way).

Bu allowing a trailing comma we get consistent formatting and nicer
diffs. If a trailing comma were forbidden, the dopping "fred=5" with
produce a diff removing not just that line but also the comma on the
preceeding line. Ugly and noisy.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Puzzling difference between lists and tuples [ In reply to ]
On Mon, Sep 21, 2020 at 10:14 PM Serhiy Storchaka <storchaka@gmail.com> wrote:
>
> 18.09.20 03:55, Chris Angelico ????:
> > On Fri, Sep 18, 2020 at 10:53 AM Grant Edwards
> > <grant.b.edwards@gmail.com> wrote:
> >> Yea, the syntax for tuple literals has always been a bit of an ugly
> >> spot in Python. If ASCII had only had one more set of open/close
> >> brackets...
> >
> > ... then I'd prefer them to be used for sets, actually. I think the
> > set/dict collision is more weird than the tuple/grouping one.
> >
> > Now, if only ASCII had *two* more sets of open/close brackets...
>
> Then I would use them for sets and frozensets.
>

Ahem. AMONG the needs for additional types of brackets are such
diverse types as.........

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list