184

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'>
8
  • 4
    ('a') just evaluates to 'a' Commented Oct 13, 2012 at 19:24
  • 3
    Wow - 3 correct answers in 3 minutes :) However, note the secret of ,: a = 1, 2, 3; print a Commented Oct 13, 2012 at 19:26
  • 41
    Brackets don't make a tuple, commas do. Commented Oct 13, 2012 at 20:28
  • 22
    @cdarke, except for the empty tuple (), which only consists in a pair of parentheses. Commented Oct 14, 2012 at 10:20
  • 2
    True, or rather, False - just about all an empty tuple is good for (if you see what I mean). Commented Oct 14, 2012 at 13:12

5 Answers 5

236

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'])
Sign up to request clarification or add additional context in comments.

7 Comments

This isn't very elegant though and looks kind of confusing. Is there any other way?
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.
@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']).
Sure, I am just saying that the tuple function doesn't negate the need for the comma.
@matanster The language syntax has not changed in this regard. AFAIK the problem is the same for Python 2 and 3.
|
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')]

Comments

16

('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')]
>>> 

Comments

0

=> 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)

1 Comment

...what does this have to do with the question being asked?
-1

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'>

4 Comments

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)
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',).
Generally preferable way in that case is to supply a list, so as to avoid trailing comma syntax. e.g. tuple(['ab'])
This method only works for strings of length 1, nothing else. The correct syntax is tuple([var])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.