0

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')
3
  • You never include id in your output tuple, only in the print statement. Was this by design? Commented Nov 23, 2015 at 9:32
  • On an off note, you will get better performance if you select unique straight from the database. You will get back less data, while also skipping the cost of uniqueness in python. It is as simple as doing cursor.execute('SELECT DISTINCT(key) FROM test2').fetchall() Commented Nov 23, 2015 at 10:15
  • @zsquare thanks. I guess changing it to a set is a little pointless when i can do it in the sql Commented Nov 23, 2015 at 10:18

1 Answer 1

4

You can do this with a nested list comprehension:

results = [(code, a[0])
           for id, number, code in first_list
           for a in second_set
           if code.startswith(a[0])]

You probably want to make the second_set a set of just the a[0] values:

second_set = {a[0] for a in second_list}

simplifying things a little in your list comprehension

results = [(code, a)
           for id, number, code in first_list
           for a in second_set if code.startswith(a)]

Your sample outputs seem to be based on the print statement, not the actual values appended to the result list. Your print statements include the id value too; if this is needed, just add it in:

results = [(id, code, a)
           for id, number, code in first_list
           for a in second_set if code.startswith(a)]
Sign up to request clarification or add additional context in comments.

10 Comments

I don't know whether I understand it correctly . Why do we need startswith here?
Plus, this seems to produce the same output as I already have?
@AkshayHazari: the original code only includes values from second_set where the code starts with that value. You still need to do that in the list comprehension.
@user47467: this produces separate (code, a) tuples. Your code produced (code, a, a, ...) tuples.
@MartijnPieters . Okay. It a substitute for the entire code. I have done it partly. 415k defeats me so bad that I've been sent to the dark ages where I can find 6 elements wood, fire, earth, metal, water and Martijn Pieters.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.