-6

Possible Duplicate:
python tuple to str

Here is the data from the database, I ran out.

>>> policy_id = ((2309L,), (118L,), (94L,))
>>> for i in policy_id:
        print i


(2309L,)
(118L,)
(94L,)

But I want the result as:

i[0]=2309
i[1]=118
i[2]=94

mysql:

 and p.policy_id in ('%s','%s','%s') %(i[0],i[1],i[2])
Community
  • 1
  • 1
a1my
  • 75
  • 1
  • 1
  • 5
  • 1
    Wait why is this posted again? You would just get the exact same answers but this will be closed before you can get them. – jamylak May 11 '12 at 10:59
  • 2
    I also recommend you to read the [Python tutorial](http://docs.python.org/tutorial/). How to access tuples and lists is basic knowledge. – Felix Kling May 11 '12 at 10:59
  • Regarding your edit: If this is what you wanted from the beginning, you should edit your original question and maybe flag it for reopen. – Felix Kling May 11 '12 at 11:00
  • 1
    Your "new question" is a duplicate of [Flattening a shallow list in Python](http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python). – Felix Kling May 11 '12 at 11:03
  • @a1my: You could have modified your original question in order to specify what you wanted to ask exactly. – glglgl May 11 '12 at 11:04

2 Answers2

3

You were close. I believe this will do what you want:

p.policy_id = ((2309L,), (118L,), (94L,))

for i in p.policy_id:
    print i[0]

yields:

2309
118
94
Levon
  • 138,105
  • 33
  • 200
  • 191
  • @a1my Just a friendly note, if this solved your problem, please consider [**accepting this answer**](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) by clicking on the checkmark next to my answer. It'll mark this problem as solved, and reward both of us with some rep points - thanks – Levon Jun 17 '12 at 19:23
0

If what you want is to have the result in a variable rather than just printing it, you could use a list comprehension:

>>> policy_id_list = [ item[0] for item in policy_id ]
>>> for i in policy_id_list:
...     print i
... 
2309
118
94
jamylak
  • 128,818
  • 30
  • 231
  • 230
martinhans
  • 1,133
  • 8
  • 18