-2

Good morning I have the following query, I make an SQL query and I get a list of tuples in response, the question is that I want to convert that list of tuples into an array to facilitate operations, since after my code requires using the array values to UPDATE in the Database

cursor = db_equipo.cursor()
sql_interface="SELECT id_interface FROM Interface WHERE id_EquipoOrigen_id=%s"
cursor.execute(sql_interface,(id_equipo,))
z=cursor.fetchall()
print(z)

((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))


At first, think about making a loop with two indexes so that you could have an index for the list, and another for the tuple, something like z [x] [y], but it is poorly optimized:

z[0][0]=3027
Z[1][0]=3028
      .
      .

And I would like something like:

[3027,3028,3029,3030 ...]
0

2 Answers 2

1

You can use a list comprehension:

[datum[0] for datum in z]

Or, if you want your code to be a bit fancy:

next(zip(*z))
Sign up to request clarification or add additional context in comments.

1 Comment

I am very grateful
0

You can use a list comprehension:

tup = ((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))

wanted_array = [entry[0] for entry in tup]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.