Home > Software design >  How to append list to list using python?
How to append list to list using python?

Time:11-25

I wonder to append listB to listA

input

listA = [[a,b],[c,d],[e,f]]
listB = [[g,h],[i,j]]

wondering output

sum_list = [[a,b],[c,d],[e,f],[g,h],[i,j]]

under my code, Please find something to fix.

    for line in vcf_file:
    match = []
    CHROM = line.split('\t')[0]
    POS = line.split('\t')[1]
    REF = line.split('\t')[3]
    ALT = line.split('\t')[4]
    VEP = line.split('ANN=')[1].split('|')[1]
    ANN = line.split('ANN=')[1].split(';')[0]
    if len(ANN.split(',')) > 1 :
        for i in ANN.split(','):
            GENE, HGVS = i.split('|')[3], i.split('|')[10]
            for one in GENE:
                tmp=[]
                tmp.append(sampleNo)
                tmp.append(GENE)
                tmp.append(CHROM)
                tmp.append(POS)
                tmp.append(REF)
                tmp.append(ALT)
                tmp.append(VEP)
            match.extend(tmp)

Any help would be appreciated. Thanks


Thank you for answering.
I resolve the problem. I made another list and extended it. It's solved.

    for line in vcf_file:
    match = []
    CHROM = line.split('\t')[0]
    POS = line.split('\t')[1]
    REF = line.split('\t')[3]
    ALT = line.split('\t')[4]
    VEP = line.split('ANN=')[1].split('|')[1]
    VEP_impact = line.split('ANN=')[1].split('|')[2]
    REF_DP = line.split('\t')[9].split(':')[1].split(',')[0]
    ALT_DP = line.split('\t')[9].split(':')[1].split(',')[1]
    AF = line.split('\t')[9].split(':')[2]
    ANN = line.split('ANN=')[1].split(';')[0]
    if len(ANN.split(',')) > 1 :
        for i in ANN.split(','):
            GENE, HGVS = i.split('|')[3], i.split('|')[10]
            ann = []
            for one in GENE:
                tmp = []
                tmp.append(sampleNo)
                tmp.append(GENE)
                tmp.append(CHROM)
                tmp.append(POS)
                tmp.append(REF)
                tmp.append(ALT)
                tmp.append(VEP)
                tmp.append(VEP_impact)
                tmp.append(REF_DP)
                tmp.append(ALT_DP)
                tmp.append(AF)
            ann.extend(tmp)
        match.extend(ann)

CodePudding user response:

There are 2 ways atleast to do this in python.

You can use the extend() or the operator.

Eg.

listA.extend(listB)

Or

listA =listB

CodePudding user response:

listA.extend(listB) should work, but it modifies the original listA. If you want sum_list to be a different list, you can copy it first with something like

sum_list = list(listA)
sum_list.extend(listB)
  • Related