Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python putting List within List when I only want one List

Im fetching a row from a table that has multiple values delimited by a ";". I'm trying to store it within a list. Everything works but instead of python printing it as just ['a','b','c']... it is printing it as [['a','b','c']].

Code below

Code
cur.execute("SELECT * FROM Main_Setup");
rows = cur.fetchall()

job1= []
job2=[]

for row in rows:
    name = row[1]
    fields = row[4].split(';')
    if name == 'File1':
        job1.append(fields)

    elif name == 'file2':
        job2.append(fields)


Output

Code
[['this field', '   another field']]
[['haha', 'no', '  yes']]

Desired Output
Code
['this field', '   another field']
['haha', 'no', '  yes']
Try extending, not appending. That should work out
ex: job1.extend(fields)
You're putting a list into an array... so that's what you'll get doing so. To access the elements you want, you'd have to use jobs2[0]. The len(jobs2) = 1. But len(jobs2[0]) == len(fields).

PHP Code

<?php 
elif name 
== 'file2':
    for 
i in fields:
        
job2.append(i) 
(Jul 13, 2017 07:51 AM)TheMarkMan Wrote:  Try extending, not appending. That should work out
ex: job1.extend(fields)

Extending worked! Thank you very much


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 630 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by S M I T H YNov 23, 2016 10:46 AM 4 177 Nov 23, 2016 10:46 AM
Last Post: God
[Python] by YakzMar 27, 2013 12:52 PM 12 470 Mar 27, 2013 12:52 PM
Last Post: Strike