473,655 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why tuple with one item is no tuple

Hi,
type(['1']) <type 'list'>
type(('1') )

<type 'str'>

I wonder why ('1') is no tuple????

Because I have to treat this "special" case differently in my code.

--
Greg
Jul 18 '05 #1
37 2371
('1',) is a tuple... you need the comma to make it a tuple.

regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #2
On Tue, 15 Mar 2005 16:16:34 GMT, Gregor Horvath <g.*******@mx.a t> wrote:
Hi,
>>>type(['1']) <type 'list'>
>>>type(('1') ) <type 'str'>

I wonder why ('1') is no tuple????
because, syntactically, those parens are for grouping, and do not
unambiguously define a tuple. It's a python gotcha. To define a
one-tuple, put a comma after the '1':
type(('1', ))

<type 'tuple'>

Because I have to treat this "special" case differently in my code.


you shouldn't have to; post your code if you still think you do.

Peace
Bill Mill
bill.mill at gmail.com
Jul 18 '05 #3
Gregor Horvath wrote:
Hi,
>>>type(['1']) <type 'list'>
>>>type(('1') ) <type 'str'>

I wonder why ('1') is no tuple????

Because I have to treat this "special" case differently in my code.


you need to tell python that ('1') isn't a string inside
a couple parens but a tuple, look:
t = ('1', )
type(t) <type 'tuple'>

if there's no ambiguity you can omit the parens:
t = '1',
type(t)

<type 'tuple'>

HTH,
deelan

--
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaf:Person ; foaf:nick "deelan" ;
foaf:weblog <http://blog.deelan.com/> .
Jul 18 '05 #4
Gregor Horvath <g.*******@mx.a t> wrote:
Hi,
type(['1'])<type 'list'>
type(('1') )

<type 'str'>

I wonder why ('1') is no tuple????


You need to say ('1',). In just plain ('1'), the parens are
interpreted as grouping, not as tuple creation. Depending on your
point of view, this is either a "special case", or an "ugly wart" in
the syntax.

a = () # tuple of zero elements
a = (1,) # tuple of one element
a = 1, # tuple of one element
a = (1) # scalar
a = (1, 2) # tuple of two elements
a = 1, 2 # tuple of two elements
a = , # syntax error

The big question is, is it the parens that make it a tuple, or is it
the comma? If you go along with the parens school of thought, then
(1,) is the special case. If you believe in commas, then the () is
the special case. In either case, it's a bit ugly, but we learn to
overlook the occasional cosmetic blemishes of those we love :-)
Jul 18 '05 #5

thanks are given to all....

"problem" solved...
--
Greg
Jul 18 '05 #6
Hmm,
going 'the other way', you are allowed an extra , but you can't have
(,) as the empty tuple.:
(1,2,) (1, 2) (1,) (1,) (,)

....
Traceback ( File "<interacti ve input>", line 1
(,)
^
SyntaxError: invalid syntax
-- Pad.

Jul 18 '05 #7
On Tuesday 15 March 2005 08:25 am, Roy Smith wrote:
a = () * * * # tuple of zero elements
a = (1,) * * # tuple of one element
a = 1, * * * # tuple of one element
a = (1) * * *# scalar
a = (1, 2) * # tuple of two elements
a = 1, 2 * * # tuple of two elements
a = , * * * *# syntax error

The big question is, is it the parens that make it a tuple, or is it
the comma? *If you go along with the parens school of thought, then
(1,) is the special case. *If you believe in commas, then the () is
the special case. *In either case, it's a bit ugly, but we learn to
overlook the occasional cosmetic blemishes of those we love :-)


The answer is obvious, the naked comma should be an empty tuple.
--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
Jul 18 '05 #8
On Tue, 15 Mar 2005 10:47:28 -0800, James Stroud <js*****@mbi.uc la.edu> wrote:
On Tuesday 15 March 2005 08:25 am, Roy Smith wrote:
a = () # tuple of zero elements
a = (1,) # tuple of one element
a = 1, # tuple of one element
a = (1) # scalar
a = (1, 2) # tuple of two elements
a = 1, 2 # tuple of two elements
a = , # syntax error

The big question is, is it the parens that make it a tuple, or is it
the comma? If you go along with the parens school of thought, then
(1,) is the special case. If you believe in commas, then the () is
the special case. In either case, it's a bit ugly, but we learn to
overlook the occasional cosmetic blemishes of those we love :-)


The answer is obvious, the naked comma should be an empty tuple.


The other answer, that parens should be required to surround all
tuples, is obvious too.

Neither is particularly appealing; a lone comma creating a data
structure seems counter-intuitive, but it's nice to do a, b = b, a
instead of (a, b) = (b, a) . In this case, since the need to create
empty tuples is vanishingly rare, I'm okay with a little
inconsistency.

Peace
Bill Mill
bill.mill at gmail.com
Jul 18 '05 #9
Bill Mill wrote:
On Tue, 15 Mar 2005 10:47:28 -0800, James Stroud <js*****@mbi.uc la.edu> wrote:
On Tuesday 15 March 2005 08:25 am, Roy Smith wrote:
> a = () # tuple of zero elements
> a = (1,) # tuple of one element
> a = 1, # tuple of one element
> a = (1) # scalar
> a = (1, 2) # tuple of two elements
> a = 1, 2 # tuple of two elements
> a = , # syntax error
>
> The big question is, is it the parens that make it a tuple, or is it
> the comma? If you go along with the parens school of thought, then
> (1,) is the special case. If you believe in commas, then the () is
> the special case. In either case, it's a bit ugly, but we learn to
> overlook the occasional cosmetic blemishes of those we love :-)


The answer is obvious, the naked comma should be an empty tuple.


The other answer, that parens should be required to surround all
tuples, is obvious too.

Neither is particularly appealing; a lone comma creating a data
structure seems counter-intuitive, but it's nice to do a, b = b, a
instead of (a, b) = (b, a) . In this case, since the need to create
empty tuples is vanishingly rare, I'm okay with a little
inconsistency.


And if you don't like it at all, you can still use tuple() to "create"
an empty tuple.

Reinhold
Jul 18 '05 #10

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

Similar topics

10
5205
by: Carlo v. Dango | last post by:
Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new tuple and copy the content of the old tuple to a new one. But how do I do this if I only at runtime know the size of the tuple? I wish I could pass around lists instead.. that would be so much easier, but I'm passing "*args" and "**kwargs" around...
50
3676
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this, I'm just curious why it is neccesary.. Thanks,
8
3622
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something with a,b, and c Now I would like to add a special fourth config value to "T": ("T",1,5,4,0.005),
1
3250
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can pickle it? Thanks , Fedor
29
3391
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I wonder why it is not implemented for tuples. George
6
1714
by: groups.20.thebriguy | last post by:
I've noticed that there's a few functions that return what appears to be a tuple, but that also has attributes for each item in the tuple. For example, time.localtime() returns a time.time_struct, which looks like a tuple but also like a struct. That is, I can do: >>> time.localtime() (2006, 1, 18, 21, 15, 11, 2, 18, 0) >>> time.localtime() 21 >>> time.localtime().tm_hour
77
4411
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick Maclaren.
25
4084
by: beginner | last post by:
Hi, I am wondering how do I 'flatten' a list or a tuple? For example, I'd like to transform or ] to . Another question is how do I pass a tuple or list of all the aurgements of a function to the function. For example, I have all the arguments of a function in a tuple a=(1,2,3). Then I want to pass each item in the tuple to a function f so that I make a function call f(1,2,3). In perl it is a given, but in python, I haven't figured out
21
7249
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP SIGNATURE-----
0
8296
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
8710
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
8497
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
8598
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
7310
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
6162
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
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2721
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
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.