How to create a "singleton" tuple with only one element
Asked Answered
M

5

175

In the below example I would expect all the elements to be tuples, why is a tuple converted to a string when it only contains a single string?

>>> a = [('a'), ('b'), ('c', 'd')]
>>> a
['a', 'b', ('c', 'd')]
>>> 
>>> for elem in a:
...     print type(elem)
... 
<type 'str'>
<type 'str'>
<type 'tuple'>
Merci answered 13/10, 2012 at 19:21 Comment(8)
('a') just evaluates to 'a'Parmesan
Wow - 3 correct answers in 3 minutes :) However, note the secret of ,: a = 1, 2, 3; print aDote
Brackets don't make a tuple, commas do.Symon
@cdarke, except for the empty tuple (), which only consists in a pair of parentheses.Innervate
True, or rather, False - just about all an empty tuple is good for (if you see what I mean).Symon
Just to clarify the existing answers, parentheses are used for grouping, so ('a') is taken as an expression that evaluates to 'a'. Tuples need a comma to be a tuple, except the empty tuple, which is empty parentheses, ().Bayard
tuple is an object delimited by comma not an object enclosed in parentheses. So a singleton tuple would be like ('a',) or tuple('a')Hughhughes
Should also be noted that (True) and (False) are boolean types so that evaluating (1 == 2) is equivalent to 1 == 2Illeetvilaine
C
226

why is a tuple converted to a string when it only contains a single string?

a = [('a'), ('b'), ('c', 'd')]

Because those first two elements aren't tuples; they're just strings. The parenthesis don't automatically make them tuples. You have to add a comma after the string to indicate to python that it should be a tuple.

>>> type( ('a') )
<type 'str'>

>>> type( ('a',) )
<type 'tuple'>

To fix your example code, add commas here:

>>> a = [('a',), ('b',), ('c', 'd')]

             ^       ^

From the Python Docs:

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.

If you truly hate the trailing comma syntax, a workaround is to pass a list to the tuple() function:

x = tuple(['a'])
Congreve answered 13/10, 2012 at 19:24 Comment(6)
This isn't very elegant though and looks kind of confusing. Is there any other way?Handcrafted
That doesn't seem to work. For example: tuple("abc") (with or without extra comma) gives ('a', 'b', 'c'), while ("abc",) gives ('abc'). So tuple() seems not to be a viable option here.Ptosis
@Ben Have a look at the docs. tuple accepts an iterable, which a string is (iterates over characters). If you insist on not using the trailing comma, then make an intermediate list: tuple(['abc']).Congreve
Sure, I am just saying that the tuple function doesn't negate the need for the comma.Ptosis
Anything more explicit variant of it in python 3.x by any chance?Cornstalk
@matanster The language syntax has not changed in this regard. AFAIK the problem is the same for Python 2 and 3.Congreve
B
26

Your first two examples are not tuples, they are strings. Single-item tuples require a trailing comma, as in:

>>> a = [('a',), ('b',), ('c', 'd')]
>>> a
[('a',), ('b',), ('c', 'd')]
Billiards answered 13/10, 2012 at 19:23 Comment(0)
S
15

('a') is not a tuple, but just a string.

You need to add an extra comma at the end to make python take them as tuple: -

>>> a = [('a',), ('b',), ('c', 'd')]
>>> a
[('a',), ('b',), ('c', 'd')]
>>> 
Sidon answered 13/10, 2012 at 19:24 Comment(0)
H
-1

=> If you need to convert list to tuple which have one id as a element. then this solution will help you.

x_list = [1]

x_tuple = tuple(x_list)

=> You will get this

(1,)

=> so now append 0 into list and then convert it into tuple

=> x_list.append(0)

=> x_tuple = tuple(x_list)

(1, 0)
Honeybunch answered 30/3, 2023 at 14:24 Comment(1)
...what does this have to do with the question being asked?Naseby
G
-2

Came across this page and I was surprised why no one mentioned one of the pretty common method for tuple with one element. May be this is version thing since this is a very old post. Anyway here it is:

>>> b = tuple(('a'))
>>> type(b)
<class 'tuple'>
Glauce answered 7/12, 2020 at 3:0 Comment(4)
Strangely, this worked for me when the other answers didn't (passing a string value to a method, so i'm using a variable and not a raw string; maybe that's why (foo,) didn't work for me)Dermato
This won't work as intended if the input string has several characters. Try, for example, tuple(('ab')) and the result will be ('a', 'b') instead of ('ab',).Laveralavergne
Generally preferable way in that case is to supply a list, so as to avoid trailing comma syntax. e.g. tuple(['ab'])Glauce
This method only works for strings of length 1, nothing else. The correct syntax is tuple([var])Embosser

© 2022 - 2024 — McMap. All rights reserved.