-1

I have a file with data:

ABC   acd   IGK  EFG

GHQ   ghq   acb  efg

IJK   ijk   gtt  ttg

I want to split its lines and take some data from each line and join them into a list. Like this:

a = ['acd', 'ghq', 'ijk'] 

So far I have done following.

li = [] 

with open('file.txt') as fl:

    for f in fl:

        f = f.split()

        li = li.append(f[2])

But I am getting the following error:

Traceback (most recent call last):

  File "<stdin>", line 4, in <module>

AttributeError: 'NoneType' object has no attribute 'append'

Can someone please help me in completing the code?

2
  • Did you even Google the error message? Commented Sep 19, 2016 at 15:42
  • Methods of mutable objects like lists and dicts that change it in-place, such as list.append() and dict.clear(), don't return the object being changed. They effectively return None.
    – martineau
    Commented Sep 19, 2016 at 17:06

1 Answer 1

1

You don't need to do li = li.append(f[2]). You only need li.append(f[2])

list.append returns none which is why you are getting the error.

Not the answer you're looking for? Browse other questions tagged or ask your own question.