postcursor.execute('SELECT customer_id FROM REQUEST') postselect_request_id_result = postcursor.fetchall() for customer_id in postselect_request_id_result: 

The problem is that in the issue, after each number there appears some kind of comma [(2,), (6,)]

the question is why?

  • A construction of the form (2,) is a “single” tuple, i.e. something of type (1,2), but with one element. So the comma is not superfluous, without it it will be just numbers in brackets, not tuples. - insolor
  • @Suliman3, you understand that this is just a representation, and as data you get quite ordinary numbers, where there are no comma-type characters at all? --- Tuples (or tuples) are usually written with such a comma at the end. The need for such formatting is due to the fact that a tul from one element could not be distinguished from grouping brackets otherwise. So that newcomers do not stumble on this, and so that the brain, while continuing to write an existing tuple, is not distracted by “closing” an already written element, it is customary to write a comma at the end. - etki
  • And how can I convert this into a list? Or is there some way to check if the tuple is pure? - Dmitry Bubnenkov
  • @ Suliman3 >>> a = (3,) >>> 3 in a True >>> 5 in a False - etki
  • Something does not work: print "----- ++ ------" print fireselect_request_id_result if 2 in fireselect_request_id_result: print "2 is HERE!" # print customer_id print "- = - = - = - =" Here is the output: ----- ++ ------ [(2,), (6,)] - = - = - = - = - Dmitry Bubnenkov 1:27 pm

2 answers 2

tuple data structure
tuple ()
so about the comma:
the comma in the case of parentheses says not only to the user, but not to the interpreter itself, that this is an iterated object, and not grouping brackets
as it was said above: consider that this is a list [[2], [6]] with the exception that it is not changeable (you can read, but you cannot write to it) and in your case (working with the "raw cursor") advise this chip:

 cur.execute("SQL STATEMENT") columns = map(lambda x: x[0], cur.description) for row in cur.fetchall(): row_as_dict = dict(zip(columns, row)) do_something() 

good luck

  • I did not quite understand what this code is doing ... - Dmitry Bubnenkov

In fact, there is no comma, the python just displays tuples from one element.

Imagine what it looks like [[2], [6]]