10

I have one problem in building a list of id using django. if i choose more than 1 id it ok but if i choose only one it will produce extra ',' in list.

testa = tuple([k['id'] for k in queryset.values('id')])
print testa

if in the queryset have exactly 1 id, it will display

(1234,)

other than that it ok

(1234,1244)

How can i remove extra ', in list'

Najmi
  • 207
  • 1
  • 3
  • 9
  • 1
    you should not ever have a `print` in your django app. –  May 21 '11 at 14:04
  • 1
    There is no extra `,` in the list. It is a tuple with one element. What is your problem exactly? If you don't want it to be printed as `(1234,)`, you can use `", ".join(str(x) for x in testa)`. – khachik May 21 '11 at 14:10
  • This is like saying "I printed the value of pi, but there was a period in it after the 3" - the comma is not really part of the tuple, it's just in there to show that it *is* a tuple. – PaulMcG May 21 '11 at 14:35
  • This is useful when using the tuple in a SQL query WHERE clause as a parameter. See https://stackoverflow.com/questions/283645/python-list-in-sql-query-as-parameter – Mithil Jan 01 '20 at 01:06

7 Answers7

17
(1234,)

is the correct Python representation of a 1-tuple. (1234) would be wrong as that is taken as a simple integer in mathematical parentheses, evaluating to 1234, not a tuple containing it.

This is different for lists because the square brackets don't have this double purpose of also meaning mathemtical order-of-operations, so whilst [1234] and [1234,] are both valid representations of a length-1-list, the default textual representation can be without the extra comma.

If you are devising your own textual representation of a tuple that does not need to be the same as Python's, you could do eg.:

'(%s)' % ', '.join(map(repr, testa))
bobince
  • 528,062
  • 107
  • 651
  • 834
3

To write a tuple containing a single value you have to include a comma, even though there is only one value.

You can index the tuple to get the desired output:

print testa[0]
>>>> 1234
garnertb
  • 9,454
  • 36
  • 38
2

actually you don't have to. the colon sign is just there in a string representation of the tuple. For example you can just create your own string by

print "(%s)" % ",".join(testa)
Constantinius
  • 34,183
  • 8
  • 77
  • 85
1

thats' how python displays the tuples with single values , also note that the recommended syntax for creating a tuple with single value is also x = (1,) that helps in differentiating a tuple from a function call . If you really want an output like you describe you can try

testa = tuple([k['id'] for k in queryset.values('id')])
if len(testa)==1:
     print '(%s)'%testa[0]
else:
     print testa
Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
  • I need the list of id to be used for sql statement.I need to use it in '..in (list of id)'.If id is single it will produce '..in (id,)' where will produce the sql error – Najmi May 21 '11 at 14:25
1

Yo say you want a list but de facto you are creating a tuple.

To get a list of ids take a look at values_list

>>> Entry.objects.values_list('id', flat=True).order_by('id')
[1, 2, 3, ...]
>>> Entry.objects.filter(id=1).values_list('id', flat=True)
[1]
arie
  • 18,737
  • 5
  • 70
  • 76
0

In python 3:

testa_tup = testa # where testa is the tuple you want to turn to string for SQL
testa = ""
for c in testa_tup:
    testa += "," + str(c)
testa = testa[1:]

Then format your SQL:

"SELECT * FROM table WHERE value IN ({testa})"
pattidegner
  • 107
  • 1
  • 10
-1
str(tuple(testa)).replace(",)",")")
Elton da Mata
  • 181
  • 1
  • 6