0

I have a big list of lists like this small example:

small example:

mylist = [['D00645:305:CCVLRANXX:2:2110:19904:74155', '272', 'chr1', '24968', '0', '32M', '*', '0', '0', 'GACAACACAGCCCTCATCCCAACTATGCACAT'], ['D00645:305:CCVLRANXX:2:2201:12674:92260', '256', 'chr1', '24969', '0', '31M', '*', '0', '0', 'ACAACACAGCCCTCATCCCAACTATGCACAT']

and I want to make a sub list of lists with the same number of inner lists. but I will change the inner lists. in the new list of lists, the inner list would have 6 columns.

1st column : the 3rd column of old inner list. they start with 'chr' 
2nd column : (the 4rh column in old inner list) - 1
3rd column : ((the 4rh column in old inner list) - 1) + length (10th column in old inner list)
4th column : the 1st column in old inner list
5th column : only 0. as integer
6th column : should be "+" or "-". if in old inner list the 2nd column is 272, in new inner list 6th column would be "-" otherwise that should be "+". 

the new list of lists will look like this:

newlist = [['chr1', 24967, 24999, 'D00645:305:CCVLRANXX:2:2110:19904:74155', 0, "-"], ['chr1', 24968, 24999, 'D00645:305:CCVLRANXX:2:2201:12674:92260', 0, "+"]]

I am trying to do that in python using the following command but it does not work like what I want. do you know how to fix it?

newlist = []
for i in mylist:
    if i[1] ==272:
        sign = '-'
    else:
        sign = '+'
    newlist.append(i[2], int(i[3])-1, int(i[3])-1+len(i[9]), i[0], 0, sign)
john
  • 263
  • 1
  • 9
  • Can you please describe how it doesn't work? What are you getting, as compared to what you want? – Mad Physicist Nov 04 '18 at 14:29
  • @MadPhysicist ¿ `TypeError: append() takes exactly one argument (6 given)` ? – gboffi Nov 04 '18 at 14:32
  • In that case, I'm voting to close as a typo. You forgot to put square brackets around the argument to `append`. – Mad Physicist Nov 04 '18 at 14:33
  • John, you can `.append` only a single item, but this single item can be, _especially in your case_, a list... `newlist.append( [i[2], ...] )` – gboffi Nov 04 '18 at 14:34
  • Please include the error in the question itself. It's crucial information without which the question is not complete. – Mad Physicist Nov 04 '18 at 14:37
  • @MadPhysicist please consider the possibility that the OP _doesn't know_ that brackets were needed... in that case the question is legitimate and the answers, albeit very simple, are correct and also useful for other folks that misunderstand the workings of `.append` :-(. – gboffi Nov 04 '18 at 14:43
  • @gboffi. Fair enough. I'm willing to retract my close vote. – Mad Physicist Nov 04 '18 at 16:40
  • @gboffi. I encourage OP to include the error information in the question even more strongly, given that it will help future readers find that they have the same problem. – Mad Physicist Nov 04 '18 at 16:43
  • @MadPhysicist OK for posting the error and the reason thereof. I feel that we can close our small debate in complete agreement! – gboffi Nov 04 '18 at 16:53

2 Answers2

2

You are appending wrongly, you are passing to the append method multiple values, where you should be appending a list.

To fix the problem, change the code in the append and wrap around it "[ ]", like so

newlist.append([i[2], int(i[3])-1, int(i[3])-1+len(i[9]), i[0], 0, sign])
1

As @Zakaria talhami answer, you need to append a list not multiple values. You can obtain the same result using list comprehension. Usually it is faster than appending an empty list. see: Why is a list comprehension so much faster than appending to a list?

Using list comprehension :

newlist = [[j[2],int(j[3])-1,int(j[3])-1+len(j[9]),j[0],0,"-" if j[1] == '272' else "+"] for j in mylist]
  • Please consider explaining why OP's procedure was failing. In this respect [this other answer](https://stackoverflow.com/a/53141917/2749397) is better than yours... – gboffi Nov 04 '18 at 17:49