CK5
CK5

Reputation: 1195

Python list values are getting overriden

I have a scenario where a python utility will inquire case ID dependencies for a case ID from a databae. Let's assume case ID 123 has dependency on case IDs 234 and 456.

There are two lists. r2 will hold empty or 1 or more than one values (dependent case ids). In turn each case ID will have versions like v1, v2, etc, and these are appended to list r3. Patch 123 will be deployed only if all versions of a case ID are deployed.

r2 = [187045, 187046] 
r3 = []

I fetch these version values from a DB log table. Everything is working fine except that a small bug is causing a problem. r3[] is getting over-ridden with new values.

for item in r2:
        cursor.execute('select distinct Version_Num from dbo.test where CMS_ID = ? and deployment_status = 0', item)
        row3 = cursor.fetchall()  #fetches a tuple
        thi_tuple = [c_tup[0] for c_tup in row3]  #converts tuple to a list
        r3 = list(map(int, thi_tuple)) #converts list to list of ints
        print(r3) 

the output is as follows

[2, 3] #versions of 187045
[1] #version of 187046

And the final output of r3 is just [1]. How to get r3 = [2, 3 ,1]?

I know that the for loop is over-riding the list with iteration.

Regards

Upvotes: 0

Views: 49

Answers (2)

Dipjyoti_Biswas
Dipjyoti_Biswas

Reputation: 51

Try this. I think it will solve your problem. Insted of

r3 = list(map(int, thi_tuple)) 

Use

r3 = r3 + list(map(int, thi_tuple)) 

You loop will now look like this:

for item in r2:
    cursor.execute('select distinct Version_Num from dbo.test where CMS_ID = ? and deployment_status = 0', item)
    row3 = cursor.fetchall()  # fetches a tuple
    thi_tuple = [c_tup[0] for c_tup in row3]  # converts tuple to a list
    r3 = r3 + list(map(int, thi_tuple))  # converts list to list of ints
    print(r3) 

Upvotes: 0

João Victor
João Victor

Reputation: 435

Where you have

for item in r2:
    cursor.execute('select distinct Version_Num from dbo.test where CMS_ID = ? and deployment_status = 0', item)
    row3 = cursor.fetchall()  #fetches a tuple
    thi_tuple = [c_tup[0] for c_tup in row3]  #converts tuple to a list
    r3 = list(map(int, thi_tuple)) #converts list to list of ints
    print(r3)

replace with

for item in r2:
    cursor.execute('select distinct Version_Num from dbo.test where CMS_ID = ? and deployment_status = 0', item)
    row3 = cursor.fetchall()  #fetches a tuple
    thi_tuple = [c_tup[0] for c_tup in row3]  #converts tuple to a list
    r3 += list(map(int, thi_tuple)) # append new values
    print(r3) 

Upvotes: 1

Related Questions