-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?

martineau
  • 119,623
  • 25
  • 170
  • 301
user3698773
  • 929
  • 2
  • 8
  • 15
  • Did you even [Google the error message](https://www.google.co.in/search?q=AttributeError%3A+%27NoneType%27+object+has+no+attribute+%27append%27)? – Ashwini Chaudhary Sep 19 '16 at 15:42
  • Methods of mutable objects like `list`s and `dict`s that change it in-place, such as `list.append()` and `dict.clear()`, don't return the object being changed. They effectively return `None`. – martineau Sep 19 '16 at 17:06

1 Answers1

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.

Farhan.K
  • 3,425
  • 2
  • 15
  • 26