How to perfectly convert one-element list to tuple in Python? [duplicate]
Asked Answered
F

9

18

So I am trying to do this:

tuple([1])

The output I expect is :

(1)

However, I got this:

(1,)

But if I do this:

tuple([1,2])

It works perfectly! like this:

(1,2)

This is so weird that I don't know why the tuple function cause this result.

Please help me to fix it.

Fundus answered 15/4, 2016 at 3:4 Comment(3)
The type for (1) is integer; while (1,) is tuple.Eryn
So (1,) just looks not perfectly. But in fact it is a tuple only contains one element?Fundus
Maybe you are not looking for a tuple, better something like: https://mcmap.net/q/740708/-how-to-remove-square-brackets-from-list-in-python-duplicateMargarite
M
16

This is such a common question that the Python Wiki has a page dedicated to it:

One Element Tuples

One-element tuples look like:

1,

The essential element here is the trailing comma. As for any expression, parentheses are optional, so you may also write one-element tuples like

(1,)

but it is the comma, not the parentheses, that define the tuple.

Mulford answered 15/4, 2016 at 3:10 Comment(3)
So (1,) just looks not perfectly. But in fact it is a tuple only contains one element?Fundus
@MarsLee Actually, 1, is a tuple that only contains one element. (1,) is just a pretty version of the same :) Try it yourself: for i in 1,2,3,: print i - the tuple is 1,2,3,, putting parenthesis around it just makes it easy for the lexer to realise it's a tuple and not confuse it with something else.Mulford
I totally got it. Really appreciate for your help. :)Fundus
W
4

use

.format(str(tuple(mwo)).replace(",)",")")))

This will replace comma from the first element.

Wigan answered 27/5, 2022 at 6:38 Comment(1)
if you just want to use it in SQL with IN operator list = ",".join(str(x) for x in [1,2]) list = f"({list})"Brandling
G
3

If you wanted to convert it to your expected representation (i.e. (1)), which as mentioned by others isn't a tuple for the singular case, you can use this approach - which will generate a string formatted output as the singular case isn't a valid tuple:

f"({str(your_list)[1:-1]})"

E.g

>>> f"({str([1])[1:-1]})"
'(1)'
>>> f"({str([1,2])[1:-1]})"
'(1,2)'
Gadabout answered 25/3, 2022 at 14:28 Comment(0)
A
2

That is how tuples are formed in python. Using just (1) evaluates to 1, just as much as using (((((((1))))))) evaluates to ((((((1)))))) to (((((1))))) to... 1.

Using (1,) explicitly tells python you want a tuple of one element

Aeroplane answered 15/4, 2016 at 3:9 Comment(0)
W
2

What you are getting is a tuple. When there is only a single element, then it has to be represented with a comma, to show it is a tuple.

Eg)

>>> a = (1)
>>> type(a)
<type 'int'>
>>> a = (1,)
>>> type(a)
<type 'tuple'>
>>>

The reason is, when you do not use a comma when there is only one element, the interpreter evaluates it like an expression grouped by paranthesis, thus assigning a with a value of the type returned by the expression

Willed answered 15/4, 2016 at 3:9 Comment(0)
S
1

From the docs

6.2.3. Parenthesized forms

A parenthesized form is an optional expression list enclosed in parentheses:

parenth_form ::=  "(" [expression_list] ")" 
A parenthesized expression list yields whatever that expression list yields: if the list contains at least one comma, it yields a tuple; otherwise, it yields the single expression that makes up the expression list.

An empty pair of parentheses yields an empty tuple object. Since tuples are immutable, the rules for literals apply (i.e., two occurrences of the empty tuple may or may not yield the same object).

Note that tuples are not formed by the parentheses, but rather by use of the comma operator. The exception is the empty tuple, for which parentheses are required — allowing unparenthesized “nothing” in expressions would cause ambiguities and allow common typos to pass uncaught.

So (1,) really is a tuple

Ship answered 15/4, 2016 at 3:12 Comment(0)
H
0

(1) is just 1 in grouping parentheses - it's an integer. (1,) is the 1-element tuple you want.

Hospitalet answered 15/4, 2016 at 3:9 Comment(0)
C
0

That is normal behavior in Python. You get a Tuple with one element. The notation (1,) is just a reminder that you got such a tuple.

Chickamauga answered 15/4, 2016 at 3:9 Comment(0)
F
0

The output (1,) is fine. The , is to mark a single element tuple.

If

a = (1) 

a is really a integer

If

a = (1, )

Then a is a tuple.

Formality answered 15/4, 2016 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.