Raiym
Raiym

Reputation: 19

Unable to append to a list

Background: I've a text files that has inputs like:

#1 GLN 15.A OE1
#1 ILE 16.A C
#1 ILE 16.A CA
#1 ILE 16.A CB
#1 ILE 16.A CD1

I convert this file into a string in python. And I get the outputs (say numbers) I want to.

A part of the code is here:

def extractnum(s):
   x = s.split('\n')
   x.pop() #To remove last line which is empty
   y = [ i.split() for i in x ]
   z = [ j[2] for j in y ] #z is the list that contains the residue and chain information
   n = [int(k.split('.')[0]) for k in z]
   a_chain_residues = ''
   ca_a_chain_residues = ''
   list_of_a_chain_residues = [ ]
   for i in list(set(z)): #list(set(z)) gives a list that removes all the duplicate items in z: it doesn't change z
      if i[-1] == 'A':
          a_chain_residues = a_chain_residues + i +','
          ca_a_chain_residues = ca_a_chain_residues + i + '.ca,'
          list_of_a_chain_residues = list_of_a_chain_residues.append(i)
      print list_of_a_chain_residues

The error in terminal reads

extractnum(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 17, in extractnum
AttributeError: 'NoneType' object has no attribute 'append'

Why can't 'i' add to a list? Is there a way to fix this?

Upvotes: 1

Views: 2998

Answers (4)

Pabitra Pati
Pabitra Pati

Reputation: 477

The return type for append method in python list is always None. append() works in-place i.e. it modifies the original list then and there.

list_of_a_chain_residues = list_of_a_chain_residues.append(i)

In the above line, no doubt, the value in i is being appended to the list list_of_a_chain_residues, but in the next moment, immediately you are assigning the return value of append to list_of_a_chain_residues (which is None). So, even though the list is getting appended, assigning itself to the return value of append() loses all the progress and makes it None. Therefore, replace the line

list_of_a_chain_residues = list_of_a_chain_residues.append(i)

simply by

list_of_a_chain_residues.append(i)

Upvotes: 0

Iman Mirzadeh
Iman Mirzadeh

Reputation: 13550

the problem is with append to list which returns None, if you do so you'll get answer

def extractnum(s):
    x = s.readlines()
    x.pop() #To remove last line which is empty
    y = [ i.split() for i in x ]
    z = [ j[2] for j in y ] #z is the list that contains the residue and chain information
    n = [int(k.split('.')[0]) for k in z]
    a_chain_residues = ''
    ca_a_chain_residues = ''
    list_of_a_chain_residues = [ ]
    for i in list(set(z)): #list(set(z)) gives a list that removes all the duplicate items in z: it doesn't change z
        if i[-1] == 'A':
            a_chain_residues = a_chain_residues + i +','
            ca_a_chain_residues = ca_a_chain_residues + i + '.ca,'
            list_of_a_chain_residues = list_of_a_chain_residues + list(i)
        print list_of_a_chain_residues

s = open("./file1.txt",'r')
extractnum(s)

Upvotes: 0

AvidLearner
AvidLearner

Reputation: 4163

Please note that:

>>>i = []
>>>print i.append(2)
None

The returned values of this append is None.

So, When doing this:

list_of_a_chain_residues = list_of_a_chain_residues.append(i)

You set list_of_a_chain_residues to be None, because append does not return a value. Do this instead:

list_of_a_chain_residues.append(i)

Upvotes: 3

Derek T. Jones
Derek T. Jones

Reputation: 1812

append does not return a value; it modifies the list directly. You will get the desired effect by simply typing:

list_of_a_chain_residues.append(i)

Upvotes: 2

Related Questions