I have a nested list issue that I cannot solve.
first_list = cursor.execute('SELECT id, number, code FROM test').fetchall()
second_list = cursor.execute('SELECT key FROM test2').fetchall()
second_set = set(second_list)
results = []
for id, number, code in first_list:
name = [code]
for a in second_set:
if code.startswith(a[0]):
if a[0] not in name:
name.append(a[0])
results.append(tuple(name))
print (id, code, name)
This produces an ouput:
('1', '98', ['1', '2'])
('2', '12', ['1', '2', '3'])
I was wondering what the best way is to do a list comprehension is, so that the output would be:
('1', '98', '1')
('1', '98', '2')
('2', '12', '1')
('2', '12', '2')
('2', '12', '3')
idin your output tuple, only in theprintstatement. Was this by design?cursor.execute('SELECT DISTINCT(key) FROM test2').fetchall()